Php only replaces the first occurrence string _ PHP Tutorial

Source: Internet
Author: User
Php only replaces the string that appears for the first time. In php, we usually replace all the specified characters in the replacement string at a time, such as the str_replace function, but sometimes we only want to replace the first occurrence, such as replacing the keyword in the article, if you want to replace the specified characters in the string in php, we usually replace all the characters at a time, such as the str_replace function, but sometimes we only want to replace the first occurrence, like replacing the keyword in the article, if there are 100 such cases that cannot be used for 100 times, I just want to limit them a few times. next I will introduce the implementation method to you.

Example

$ Str = 'this is the string. I will only replace ABC once and I will not replace it. is there any way to implement this. ';

Replace the first abc with xyz. because the string to be replaced is fixed, many people think of using the str_replace () function to see if this function is required.

Str_replace (mixed $ search, mixed $ replace, mixed $ subject [, int & $ count])

Accidentally, I thought it was what we wanted. the last parameter was the total number of times the replacement was returned. it was a reference variable, instead of the number of times it would be replaced, therefore, str_replace () cannot be used.

Preg_replace () can be implemented. Unfortunately, regular expressions are used,

The code is as follows:

$ Str = preg_replace ('/abc/', 'ABC', $ str, 1 );
Echo $ str;

Example

Show email as four digits forward from the first two digits (inclusive) @

The code is as follows:

Function show_email_2 ($ string ){
$ First = strpos ($ string ,'@');
// Var_dump ($ first );
If ($ first = 1 ){
$ String = '****'. $ string;
}
If ($ first> 1 & $ first <= 5 ){
$ String = substr_replace ($ string, '*****', 0, $ first-1 );
}
If ($ first> 5 ){
$ String = substr_replace ($ string, '*****', $ first-5, 4 );
}

Var_dump ($ string );
Return $ string;
}
// Show_email_2 ('22 @ 163.com '); // output --> *** 2@163.com
// Show_email_2 ('22 @ 22.com '); // output --> *** 2@22.com
Show_email_2 ('1970 @ 163.com '); // output --> 61 **** 6@163.com

Is there any regex? well, you can.

$ Replace = 'XYZ ';
If ($ position = strpos ($ str, $ replace ))! = False ){
$ Leng = strlen ($ replace );
$ Str = substr_replace ($ str, 'ABC', $ position, $ leng );
}
Echo $ str;

If you want to replace it with a specified number of times, refer to the following method:

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
?>

The instance is easy to understand. if you don't want to know more about it, we can simply use str_replace.

...

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.