Home  >  Article  >  Backend Development  >  How to implement regular string replacement in php

How to implement regular string replacement in php

藏色散人
藏色散人Original
2020-11-23 09:11:442361browse

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.

How to implement regular string replacement in php

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

It is best to use regular replacement

	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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn