Hongmu Notes
Home Language Notes In PHP regular expressions, how do you replace a character when the string you want to replace contains the '|' character itself?
Language Notes PHP

In PHP regular expressions, how do you replace a character when the string you want to replace contains the '|' character itself?

In PHP regular expressions, how do you replace a character when the string you want to replace contains the '|' character itself?

In PHP, you can use preg_replace() Use a function to replace tab characters in a string. In regular expressions, a tab character is represented as |To replace tabs with other characters, you can use $1 A symbol representing the replaced character; for example:

$str = "这是一段 | 字符";  
$str = preg_replace('/\|/', '替换后的字符', $str);  
echo $str; // 输出 "这是一段替换后的字符";

In the above example, the regular expression /\|/ Match Tab Character |Then use it. $1 A symbol indicating which character the tab character has been replaced by. Within the replacement function,'' Indicates that the string to be replaced is empty.'替换后的字符' Specify the character to replace with; in this case, it is '替换后的字符'

If the string to be replaced contains multiple tab characters, you can use this method. /\|\|/ Use a regular expression to match two tabs, then apply it. $1 A symbol indicating what character the first tab character was replaced by; for example:

$str = "这是一段 | 字符 | 还有一段";  
$str = preg_replace('/\|\|/', '替换后的字符', $str);  
echo $str; // 输出 "这是一段替换后的字符还有一段";

In the above example, the regular expression /\|\|/ Matches two tabs. |Then use it. $1 A symbol indicating which character the first tab character was replaced by. Within the replacement function,'' Indicates that the string to be replaced is empty.'替换后的字符' Specifies the character to replace it with.

微信赞赏

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…
👁 140
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']=…
👁 187
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

(Adaptive Mobile Version) Smart Robot Website Template – AI Artificial Intelligence Website Source Code Download – 1125

(Adaptive Mobile Version) Smart Robot Website Template – AI Artificial Intelligence Website Source Code Download – 1125 Practical Collection pbootcms Template

An intelligent robot-themed AI-powered PbootCMS website template supporting both PC and WAP devices. Its cutting-edge design is ideal for showcasing intelligent robotics products, AI technologies, and innovative solutions. This template helps AI enterprises present their brands online and attract investment and customers. Template Overview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5....
👁 47
Firefox web development and debugging enable forced refresh cache mode

Firefox web development and debugging enable forced refresh cache mode Summary of pitfalls

Clearing the cache in Fox Browser can be tricky; using shortcuts like Ctrl+F5 or Ctrl+R doesn't work in Google Chrome or Internet Explorer. Clearing the cache in Firefox is also quite cumbersome—the default shortcut (Ctrl+Shift+Del) opens a pop-up window for selective cleanup, requiring additional clicks to confirm; this is very inconvenient and not ideal for web development and debugging. To disable local caching in Firefox, you need to go to its Preferences and configure the settings accordingly: Firefox Browser...
👁 321
(Adaptive Mobile Version) Marketing-Driven Responsive Website Template for Water Supply and Environmental Protection Equipment Manufacturing Companies (PBootCMS) – Download Source Code for Stainless Steel Water Tank Water Treatment Websites – 0396

(Adaptive Mobile Version) Marketing-Driven Responsive Website Template for Water Supply and Environmental Protection Equipment Manufacturing Companies (PBootCMS) – Download Source Code for Stainless Steel Water Tank Water Treatment Websites – 0396 Practical Collection pbootcms Template

A PbootCMS corporate website template designed for manufacturing companies specializing in adaptive, marketing-oriented water supply and environmental protection equipment. The professional, tech-driven design is ideal for showcasing products such as stainless steel water tanks and water treatment systems. This template helps environmental protection equipment manufacturers effectively demonstrate their capabilities online and attract engineering clients. Template Overview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn (Related resources...)
👁 55
Fruit and Vegetable Agricultural Planting Base Website Template 0706

Fruit and Vegetable Agricultural Planting Base Website Template 0706 Practical Collection Yiyou template

An EyouCMS website template designed for agricultural farming bases specializing in melons, fruits, and vegetables. Its fresh and natural design effectively showcases the cultivation environment, agricultural products, harvesting experiences, and brand stories. This template helps agricultural enterprises showcase their products online and expand their sales channels. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS (Ey...
👁 61
(Adaptive Mobile Version) Medical Supplies Website Template – Download Medical Supplies Website Source Code – 1126

(Adaptive Mobile Version) Medical Supplies Website Template – Download Medical Supplies Website Source Code – 1126 Practical Collection pbootcms Template

A PbootCMS website template for medical supplies and healthcare products, compatible with both PC and WAP devices. Featuring a professional, tech-oriented design ideal for showcasing medical supplies, healthcare products, and health solutions. This template enables healthcare enterprises to display their products online and attract hospitals and government clients. Template Overview | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 50