Hongmu Notes
Home Language Notes Use PHP regular expressions to replace specified content – replace it only once.
Language Notes PHP

Use PHP regular expressions to replace specified content – replace it only once.

Use PHP regular expressions to replace specified content – replace it only once.

To use regular expressions in PHP to replace specific content exactly once, you can use the `preg_replace()` function. preg_replace_callback() Function. This function accepts three parameters: the string to be replaced, the replacement string, and a callback function. The callback function is a custom function that is executed during the regular expression search and replacement process.

Here is an example:

$string = "Hello, world! This is a PHP question.";    
$pattern = "|"; // 用 | 分隔的字符串    
$replacement = "!";    
$result = preg_replace_callback($pattern, function($match) {    
    if ($match[0] == "Hello") {    
        return "Hello!";    
    } else if ($match[0] == "world") {    
        return "World!";    
    } else if ($match[0] == "This") {    
        return "This!";    
    } else {    
        return $match[0];    
    }    
}, $string);    
echo $result; // 输出 "Hello! World! This!"

In this example, we use $pattern Use the `String` method to find the strings "Hello", "world" and "This" and replace them with their corresponding exclamation marks. If you only want to replace the text between "Hello" and "world", you can use code similar to the following:

$string = "Hello, world! This is a PHP question.";    
$pattern = "(Hello|world)!";    
$replacement = "$1!";    
$result = preg_replace($pattern, $replacement, $string);    
echo $result; // 输出 "Hello! World! This!"

In this example, we use $pattern Use parameters to search for the strings "Hello" or "world" and replace them with their corresponding exclamation marks. Since we only want to replace the text between "Hello" and "world", we use a regular expression for this purpose. (Hello|world) Use this to match the two strings. In the callback function, we use $match[0] Retrieve the matching strings and replace them with the corresponding exclamation marks.

微信赞赏

WeChat

支付宝赞赏

Alipay

✍️ Author: Hong Mu

webmaster · Thanks for reading, stay tuned for more exciting content

Author homepage View home page →

Related articles

PHP ob function record

PHP ob function record Language Notes PHP

Usage of the following three functions ob_get_contents(); ob_end_clean(); ob_start(); You can use these functions to buffer local files and execute local script code. Use ob_start() to save the output code into the buffer, and the page will not be displayed; then use ob_get_contents to get the data in the buffer. o…
👁 141
PHP preg

PHP preg Language Notes PHP

The preg_match_all function is used to perform a global regular expression match. preg_match_all() Syntax int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG…
👁 169
Detailed explanation of PHP ternary operator and if

Detailed explanation of PHP ternary operator and if Language Notes PHP

Ternary operator condition ? Result 1 : Result 2 Explanation: The position in front of the question mark is the condition for judgment. If the condition is met, the result is 1, and if it is not met, the result is 2. This article compares and explains the ternary operator and if...else... in detail. I hope it will be helpful to everyone. Today when I was revising my paper online, I encountered a statement that I couldn’t understand: $if_summary = $row['IF_SUMMARY']=…
👁 189
PHP cast type

PHP cast type Language Notes PHP PHP collection PHP and mysql

Get the data type 1. If you want to check the value and type of an expression, use var_dump(). 2. If you just want to get an easy-to-read type expression for debugging, use gettype(). 3. To check a certain type, do not use gettype(), but use the is_type() function. Converting Strings to Numbers When a string is evaluated as a number, the result is determined according to the following rules...
👁 232

Recommended reading

Responsive greenery and floral website template – 0716

Responsive greenery and floral website template – 0716 Practical Collection Yiyou template

An eYouCMS responsive website template designed for the greenery and floriculture industries. Its fresh and natural design is ideal for showcasing plant and floral products, cultivation techniques, brand stories, and gardening services. This template helps floral and greenery brands effectively present their products online, attracting both individual and corporate customers. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Common Questions About eYouCMS Installation...
👁 46
(Adaptive mobile version) Blue responsive environmental engineering equipment PBootCMS website template – Download HTML5 environmental equipment website source code – 0402

(Adaptive mobile version) Blue responsive environmental engineering equipment PBootCMS website template – Download HTML5 environmental equipment website source code – 0402 Practical Collection pbootcms Template

An adaptive, blue-responsive website template for PbootCMS, designed for environmental engineering and eco-friendly equipment projects, built using HTML5 technology. Its professional, tech-oriented design is ideal for showcasing environmental engineering projects, equipment products, and solutions. This template helps environmental engineering companies demonstrate their technical expertise online and attract potential clients. Template Overview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: w...
👁 51
(Adaptive mobile version) Universal website template for foreign trade companies – featuring automatic translation functionality – 1134

(Adaptive mobile version) Universal website template for foreign trade companies – featuring automatic translation functionality – 1134 Practical Collection pbootcms Template

A versatile PbootCMS website template designed for international trade companies, compatible with both PC and WAP devices, and featuring an automatic translation function. Its international and professional design makes it ideal for foreign trade enterprises to create multilingual official websites. The automatic translation feature enables businesses to promote their brands at low cost across global markets. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.c...
👁 45
Responsive Landscape Flower and Plant Sculpture Website Template 0717

Responsive Landscape Flower and Plant Sculpture Website Template 0717 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for the landscaping, floral, and plant sculpture industry. Its natural and artistic design style is perfect for showcasing floral products, garden sculptures, landscape designs, and brand stories. It helps landscaping businesses showcase their works online and attract customers. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 59
(Adaptive mobile version) Blue atmospheric chemical filter media – Stone manufacturer; PBootCMS template – Abrasives production website source code download – 0403

(Adaptive mobile version) Blue atmospheric chemical filter media – Stone manufacturer; PBootCMS template – Abrasives production website source code download – 0403 Practical Collection pbootcms Template

This set includes an adaptive mobile-friendly PbootCMS website template designed for the production of blue industrial chemical filter media and abrasives. The professional and sophisticated design is ideal for showcasing chemical filter media, abrasive products, and industrial applications. It helps chemical materials companies demonstrate their production capabilities online and attract industrial customers. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn – Related...
👁 42