The PHP string replacement method can be directly processed with the regular expression, but the regular expression is not familiar to all PHP designers, so PHP also has several ready-made functions that can be used to perform string substitution, the commonly used preg_replace , substr_replace and str_replace are all. You can use them to complete simple string replacement without writing formalities. You only need to take parameters. There are two concepts in the PHP string replacement method using these functions, which is to search for the string content to be replaced first. The next step is to replace the old string with the new string . We will show you an example. First, let's understand the basic grammatical rules of preg_replace , substr_replace and str_replace . Basic grammar rules PHP preg_replace Basic grammar (for detailed usage see: PHP preg_replace )
preg_replace ($pattern, $replacement, $subject, $limit = -1, $count)
Parameter description: Search the content of $subject and find the part of $pattern, replace it with $replacement.Basic syntax of PHP substr_replace (for detailed usage, please refer to: PHP substr_replace function )substr_replace ($string, $replacement, $start, $length)
Parameter description: $string is the original string, $replacement is the new string to be replaced, $start is the position where the original string is to be replaced, and $length is the length of the string to be replaced.PHP str_replace basic syntax (for detailed usage refer to: PHP str_replace function )str_replace( search, replace, subject, count)
Parameter description: search is the string to be replaced, replace is the new string to be replaced, subject is the original string, count is the parameter to count the number of replacements, select.Practical application example of PHP string substitution method<?php
$OriginalString="This is original string.";
echo preg_replace("/original/",'new',$OriginalString).'<br>';
echo substr_replace($OriginalString,'new',8,8 ).'<br>';
echo str_replace('original','new',$OriginalString);
?>
Example string substitution resultThis is new string.
This is new string.
This is new string.
In the example, we used three functions to process the string replacement of the same original string. Replace the English word original in the original string with the English word new. From the example string replacement results, we can see that all succeeded. Replace, these three parameters are the commonly used parameters in the PHP string replacement method.
These three functions seem to be similar in use, but they are completely different in the way of parameter setting, and there are some more suitable timing points. In particular, substr_replace and str_replace can handle array elements in addition to string replacement. I suggest you spend some time Time to study their differences, and then it will be more convenient to decide which parameter to use for string substitution.
- PHP preg_replace
- PHP substr_replace function
- PHP str_replace function
Post a Comment
0 Comments