Home > Article > Backend Development > How to implement regular string replacement in php
How to implement regular string replacement in php: first create a PHP sample file; then use the expression "preg_match_all('/(http:\/\/blog.com).*?(php)/is ',"aaaa..." Just implement string replacement.
## Recommended: "PHP Video Tutorial》
The operating environment of this tutorial: windows7 system, PHP5.6 version, this method is suitable for all brands of computers.
Implementation method of php regular string replacement:
The requirement is to add some strings after a specific string in a piece of content
preg_match_all('/(http:\/\/blog.com).*?(php)/is',"aaaahttp://blog.com/sss/index.phpsdsdahttp://blog.com/dssssss/index.phpsdhttp://blog.com/dssssss/index.phpsd", $data); var_dump($data);die;
The key question is how to write the regular expression
Here is .* Match any string? (php) Stop when hitting php/// //到时补充下有关正则 //------------------------------------ ---Redundant----------------------------------Learn Several string manipulation functions
strpos Find the string position
$newstring = 'abcdef abcdef'; $pos = strpos($newstring, 'a', 1); // $pos = 7, 不是 0 第三个参数是标记从哪里开始找There is a way to find all occurrences
function getCharpos2($str, $char){ $j = 0; $arr = array(); $count = substr_count($str, $char); for($i = 0; $i < $count; $i++){ $j = strpos($str, $char, $j); $arr[] = $j; $j = $j+1; } return $arr; }
str_replace 这个没什么好说的 就是替换This method works for all brands of computers.
The above is the detailed content of How to implement regular string replacement in php. For more information, please follow other related articles on the PHP Chinese website!