How to replace character strings in PHP _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Some character methods in replacement strings in PHP are introduced. Replacing strings in php will all use the str_replace function. This function can also use regular expressions, the following small series will introduce some characters in the replacement string or replace the first character in php. we will all use the str_replace function. This function can also use regular expressions, the following section describes how to replace some characters in a string or an example of the first occurrence of a character.

Now there is A need: string A and string B, string B contains string A, use string A to replace string B with another string or delete it.

Using PHP functions, str_ireplace () and str_replace () can be used.

1. the str_ireplace (find, replace, string, count) function uses one string to replace other characters in the string (this function is not case sensitive ).

For example:

The code is as follows:

Header ("Content-Type: text/html; charset = utf-8"); // prevents Chinese garbled characters
$ Str_1 = 'G bowl w p bowl P ';
$ Str_2 = '? Pan? ';
$ Str_3 = 'php replaces some strings in the string-Guo G bowl w p basin P ';

$ Str = str_ireplace ($ str_1, $ str_2, $ str_3 );
Echo $ str;
// Output: PHP replaces some strings in the string -? Pan
?>

2. the str_replace (find, replace, string, count) function uses one string to replace other characters in the string (this function is case sensitive ).


(The parameter and description are the same as the str_ireplace () function)

The code is as follows:

Header ("Content-Type: text/html; charset = utf-8"); // prevents Chinese garbled characters
$ Str_1_s = 'Guo g bowl w p basin P ';
$ Str_1_ B = 'Guo G bowl w p basin P ';
$ Str_2 = '? Pan? ';
$ Str_3 = 'php replaces some strings in the string-Guo G bowl w p basin P ';

$ Str_s = str_replace ($ str_1_s, $ str_2, $ str_3 ).'

';
$ Str_ B = str_replace ($ str_1_ B, $ str_2, $ str_3 );
Echo $ str_s; // the source string cannot be found.
Echo $ str_ B; // is replaced correctly
// $ Str_s output: PHP replaces some strings in the string-Guo G bowl w p pot P
// $ Str_ B output: PHP replaces some strings in the string -? Pan
?>

The replacement above must have all been replaced. what if I want to replace only the first character that appears?

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/', 'XYZ', $ str, 1 );
Echo $ str;

Is there any regex? well, you can.

The code is as follows:

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

...

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.