How to implement regular string replacement in php

藏色散人
Release: 2023-03-07 18:42:02
Original
2306 people have browsed it

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;
Copy after login

The key question is how to write the regular expression

Here is .* Match any string? (php) Stop when hitting php

///
//到时补充下有关正则
//
Copy after login

------------------------------------ ---Redundant----------------------------------

Learn Several string manipulation functions

strpos Find the string position

$newstring = 'abcdef abcdef';
$pos = strpos($newstring, 'a', 1); // $pos = 7, 不是 0  第三个参数是标记从哪里开始找
Copy after login

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;
}
Copy after login
str_replace 这个没什么好说的 就是替换
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!