search
HomeBackend DevelopmentPHP ProblemHow 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.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft