Php replaces the nth character code in the string. Below I have summarized some character replacement methods in php development, including direct replacement without regular expressions or replacement with regular expressions, and replacement using php built-in functions. For example, below is a summary of some character replacement methods in php development, including direct replacement without a regular expression or replacement with a regular expression, and replacement with a php built-in function.
For example, there is a string: $ a = 'Hello world hello pig hello cat hello dog hello small boys ';
Then we want to change the 3rd hello appearance to good-bye, for example:
'Hello world hello pig good-bye cat hello dog hello small boys ';
In this case, I will not find the PHP built-in function at half past one, and I wrote this simple small function when the regular expression is not required, if you have recommended built-in functions, leave a message :)
The code is as follows: |
|
/* * $ Text is the input text; * $ Word is the original string; * $ Cword is the string to be replaced; * $ Pos indicates the position where $ word appears N times in $ text, starting from 1. * */ Function changeNstr ($ text, $ word, $ cword, $ pos = 1 ){ $ Text_array = explode ($ word, $ text ); $ Num = count ($ text_array)-1; If ($ pos> $ num ){ Return "the number is too big! Or can not find the $ word "; } $ Result_str = ''; For ($ I = 0; $ I <= $ num; $ I ++ ){ If ($ I ==$ pos-1 ){ $ Result_str. = $ text_array [$ I]. $ cword; } Else { $ Result_str. = $ text_array [$ I]. $ word; } } Return rtrim ($ result_str, $ word ); } $ Text = 'Hello world hello pig hello cat hello dog hello small boys '; $ Word = 'hello '; $ Cword = 'good-bye '; Echo changeNstr ($ text, $ word, $ cword, 3 ); // Output: hello world hello pig good-bye cat hello dog hello small boy ?> |
Regex will be more efficient
If it is UTF-8 encoded
The code is as follows: |
|
$ Regex = "/(, |, |)/I "; $ Test = "Hebei, Shijiazhuang, Beijing, Shanghai | Tianjin | Chongqing | Baoding, Henan ,"; $ Result = preg_replace ($ regex, "", $ test ); Print_r ($ result ); ?> |
Result
Hebei Shijiazhuang Beijing Shanghai Tianjin Chongqing Baoding Henan
Php function replacement
Differences between common regular expression matching functions in PHP include str_replace, str_ireplace, substr_replace, preg_replace, preg_match, preg_match_all, preg_quote, preg_split, ereg_replace, delimiter, preg_replace, str_split, of course, a few of them cannot use regular expressions, but because of their ambiguous relationship with Related regular functions, they are all put together for comparison.
|
Supports regular expressions |
Features |
Remarks |
Str_replace |
X |
String replacement function, Case Sensitive |
|
Str_ireplace |
X |
String replacement function. it is case insensitive and supports array-based batch replacement. |
Thanks to franci for reminding me to add |
Substr_replace |
X |
Partially replaces the string function. you can specify the position index. |
|
|
|
|
|
Preg_replace |
Y |
Specifies the matching mode to replace, and supports sub-string reference. |
Priority |
Ereg_replace |
Y |
Replace the specified matching mode. it is case sensitive and supports substring reference. |
|
Eregi_replace |
Y |
Replace the specified matching mode, which is case insensitive and supports substring reference. |
|
|
|
|
|
Ereg |
Y |
Specifies full-text match of the pattern, which can be used for matching and judgment, or returns a matching array. |
|
Preg_match |
Y |
Specifies the exit of a pattern match, which can be used to determine whether to match or use the returned matching array. |
Priority |
Preg_match_all |
Y |
Specifies full-text match of the pattern, which is generally used to use the returned matching array |
Priority |
|
|
|
|
Preg_split |
Y |
Specify regular partitioning in matching mode. it is best to use explode or str_split if it can be used. |
|
Str_split |
X |
Split string with specified length. by default, a single character is split into arrays. |
|
Explode |
X |
You can specify one or more characters to split the string. if the string is successfully split, an array is returned. for example, if the string is split by 34, 12 and 5 are returned. |
|
|
|
|
|
Preg_quote |
- |
Escape regular expression characters, which means adding a backslash to a special character. the special characters of a regular expression include:. + *? [^] $ () {}=! <> | :- |
|
|
|
|
|
Bytes. For example, there is a word...