Php replaces the nth character code in the string

Source: Internet
Author: User
Tags explode

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: Copy code

<? Php
/*
* $ 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: Copy code

<? Php
$ 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:. + *? [^] $ () {}=! <> | :-  
       

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.