Use of strings
This post is reprinted from: http://blog.9cbs.net/shenzhe__/archive/2004/07/31/57102.aspx
First, the use of strings
1. I want to access or change part of the string instead of the entire string
Substr () and substr_replace () functions. A part of the readable writing string.
$ SUB_STR = Substr ($ STR, $ Initial_pos);
$ SUB_STR = Substr ($ STR, $ INITIAL_POS, $ STR_LEN);
$ new_str = substr_replace ($ STR, $ Replacement, $ Initial_POS);
$ new_str = Substr_replace ($ STR, $ Replacement, $ INITIAL_POS, $ STR_LEN);
? >
The strpos () function can give the specified character in the first place in the string:
$ Email = 'shenzhe163@sohu.com';
$ usrname = sub ($ Email, 0, Strop ($ Email, '@'));
?>
Substr () and EREG () functions can match part of the string
IF (EREG ("YouPattern", Substr ($ String, -15))) {
Print "The Last 15 Characters of String Match The Pattern";
}
?>
The unpack () function extracts binary data from the string and put it into a related array.
// read Five Characters and assign to $ REC ["zipcode"];
$ REC = Unpack ("A5zcode", $ STR);
// Same Thing Whit Substring
$ REC ["Xipcode"] = SUBSTR ($ STR, 0, 5);
?>