Hongmu Notes
Home Language Notes Use PHP regular expressions to replace specified content exactly once (Method 2)
Language Notes PHP

Use PHP regular expressions to replace specified content exactly once (Method 2)

Use PHP regular expressions to replace specified content exactly once (Method 2)

Can be used in PHP. preg_replace This function performs a single regular-expression replacement on specified content; it uses a regular expression to search through a string and replaces all matching occurrences with the specified replacement string.

Here is an example code snippet for replacing the first matching occurrence in a string with a specified value:

$string = 'This is a test string.';
$pattern = '/is/';
$replacement = 'at';
$result = preg_replace($pattern, $replacement, $string, 1);

echo $result;

In the above code, we have defined a string. $stringA regular expression $pattern and a replacement string $replacementThen, we use it. preg_replace Use a function for search $string First match $pattern The location, and use $replacement Replace it. The fourth parameter of the function is optional and is used to specify the maximum number of replacements. In this example, we set it to 1 so that only the first matching occurrence is replaced.

The output will be:That is a test string.The first one is Replaced with at

微信赞赏

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…
👁 143
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']=…
👁 191
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

Flower garden planting display website template 0714

Flower garden planting display website template 0714 Practical Collection Yiyou template

An eyoucms website template targeting the flower garden planting and display industry. The design style is fresh and beautiful, and can display flower products, garden landscapes, planting techniques and brand stories. It helps flower and garden companies display products online and attract customers. Template display Installation instructions Website backend: /login.php Account: admin Password: admin Related articles Yiyou CMS installation FAQ summary Yiyou CMS (…
👁 61
(Adaptive Mobile Version) Network Technology Company Template – IT Website Development Source Code Download – 1132

(Adaptive Mobile Version) Network Technology Company Template – IT Website Development Source Code Download – 1132 Practical Collection pbootcms Template

A website template developed by a web technology company for IT-based website building using PbootCMS, compatible with both PC and WAP devices. Featuring a modern, tech-driven design, this template is ideal for web technology companies to showcase their case studies, services, and technical expertise. It helps internet companies attract corporate clients online and enhance their brand awareness. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5....
👁 58
(Adaptive mobile version) Blue Kitchen & Bathroom Equipment Website – PBootCMS Template; Download Website Source Code for Kitchen Faucet Valves & Sink Systems – 0401

(Adaptive mobile version) Blue Kitchen & Bathroom Equipment Website – PBootCMS Template; Download Website Source Code for Kitchen Faucet Valves & Sink Systems – 0401 Practical Collection pbootcms Template

This set includes adaptive mobile-friendly blue kitchen and bathroom equipment and a PbootCMS website template for kitchen faucet valves. The design features a professional, modern style, making it ideal for showcasing kitchen and bathroom hardware products as well as home decoration applications. It helps kitchen and bathroom brands showcase their products online, attracting both renovation companies and individual homeowners. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn | Related Articles...
👁 61
Flower gardening planting and picking website template 0715

Flower gardening planting and picking website template 0715 Practical Collection Yiyou template

This set of eyoucms templates is suitable for the flower horticulture planting and picking industry. The design style is fresh and natural, and can display flower products, horticulture planting, picking experience and brand stories. Helps gardening companies to display products online and attract family and leisure customers. Template display Installation instructions Website backend: /login.php Account: admin Password: admin Related articles Yiyou CMS installation FAQ summary Yiyou CMS...
👁 43
(Adaptive mobile version) Buddhist Temple Website Template – Download Buddhist Temple Website Source Code – 1133

(Adaptive mobile version) Buddhist Temple Website Template – Download Buddhist Temple Website Source Code – 1133 Practical Collection pbootcms Template

A PbootCMS website template designed for Buddhist temples and Buddhist-related institutions, compatible with both PC and WAP devices. Its dignified and classical design makes it ideal for temples and Buddhist cultural organizations to showcase their religious activities and cultural heritage. This template helps Buddhist cultural promotion organizations build a strong online presence and attract devotees. Template Preview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 53