PHP replaces the nth occurrences of character codes in a string _php tutorial

Source: Internet
Author: User
Here's a summary of some of the character substitution methods in PHP development, including the use of regular replacement or regular substitution characters and the use of PHP's own functions for substitution.

For example there is a string: $a = ' Hello world hello pig hello cat Hello dog hello small boy ';
Then I want to change the 3rd occurrence of Hello into good-bye, for example:
' Hello World Hello pig good-bye cat Hello dog hello small boy ';
In this case, I will not find the PHP built-in function 1:30, and in the case of requirements can not use regular expressions, it is written this simple small function, if you have a good built-in function recommendation, welcome message:)

The code is as follows Copy Code

/*
* $text is the text entered;
* $word is the original string;
* $cword is a string that needs to be replaced;
* $pos refers to the position of the nth occurrence of $word 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 boy ';
$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 regular will be more method


If it's utf-8 encoded,

The code is as follows Copy Code

$regex = "/(, |,| | | |)/I ";
$test = "Hebei, Shijiazhuang, Beijing, Shanghai | tianjin | | | chongqing | baoding,,, Henan,";
$result = Preg_replace ($regex, "", $test);
Print_r ($result);
?>


Results
Hebei Shijiazhuang Beijing Shanghai Tianjin Chongqing Baoding Henan

PHP function substitution

PHP common Regular matching function between the differences, mainly Str_replace, Str_ireplace, Substr_replace, Preg_replace, Preg_match, Preg_match_all, Preg_quote, Preg_split, Ereg_replace, Eregi_replace, Preg_replace, Str_split, of course, a few of them can not use regular expressions, but because of the correlation with the regular function ambiguous so all put together to compare

Support Regular Characteristics Note
Str_replace X String substitution function, case sensitive
Str_ireplace X String substitution function, case insensitive, array-type batch substitution support Thank netizens Franci, remind to add
Substr_replace X Partial substitution string function, you can specify the position index
Preg_replace Y Specifying a matching pattern for substitution, supporting substring references Priority use
Ereg_replace Y Specify matching pattern for substitution, case sensitivity, support for substring references
Eregi_replace Y Specifies that the matching pattern is replaced, case insensitive, and supports substring references
Ereg Y Specifies the pattern full-text match, which can be used to match the judgment, or return a matching array
Preg_match Y Specifies whether the pattern matches one exit, which can be used to match the judgment, or to use the returned matching array Priority use
Preg_match_all Y Specifies a pattern full-text match, typically used to return a matching array Priority use
Preg_split Y Specifies the regular split in the matching mode, if it is best to use explode or str_split
Str_split X Specifies the length of the split string, by default a single character split into an array
Explode X You can specify a single or multiple character splitting string, and a successful return array, for example, 12345 returns 12 and 5 according to the 34 split
Preg_quote - Escape regular expression characters, meaning that special characters are prefixed with backslashes, and special characters for regular expressions include:. + * ? [ ^ ] $ ( ) { } = ! < > | : -

http://www.bkjia.com/PHPjc/629041.html www.bkjia.com true http://www.bkjia.com/PHPjc/629041.html techarticle Here's a summary of some of the character substitution methods in PHP development, including the use of regular replacement or regular substitution characters and the use of PHP's own functions for substitution. For example, there is a word ...

  • 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.