Hongmu Notes
Home Language Notes PHP Character Replacement / 20230101/ This date is not fixed $_POST[newstext]=str_replace('/file/20230101/','/file/',$_Post[newstext]);
Language Notes PHP

PHP Character Replacement / 20230101/ This date is not fixed $_POST[newstext]=str_replace('/file/20230101/','/file/',$_Post[newstext]);

PHP Character Replacement / 20230101/ This date is not fixed $_POST[newstext]=str_replace('/file/20230101/','/file/',$_Post[newstext]);

Regular expressions can be used to match arbitrary dates and replace them with an empty string; the code is as follows:

$_POST['newstext'] = preg_replace('/\/file\/\d{8}\//', '/file/', $_POST['newstext']);

Regular expressions are used here./\/file\/\d{8}\//Match something similar to/file/20230101/Such a string, where\d{8}Represents an 8-bit numeric value. Then use it.preg_replace()The function replaces the matched string with/file/Note: The slash in a regular expression must be escaped.

If you are not familiar with regular expressions, you can refer to online regular expression tutorials and tools, such as RegexOneRegExr

The date is not fixed; sometimes it represents a string of unknown length.

If the date-part string has an unknown length, you can use a regular expression to match a substring within it; the code is as follows:

$_POST['newstext'] = preg_replace('/\/file\/\w+\//', '/file/', $_POST['newstext']);

The regular expression here/\w+/Matches one or more letters, digits, or underscore characters, representing the date portion. Then usepreg_replace()The function replaces the matched string with/file/Similarly, the slashes before and after the matched string should also be included.

Note: This method may replace strings that do not contain date components, for example./file/abc123/Therefore, it is necessary to determine whether to use this replacement method based on the specific circumstances.

微信赞赏

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 Drive Chip – Electrical Website Template 1027

Responsive Drive Chip – Electrical Website Template 1027 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for the driver chip and electrical industries. Its modern, precision-oriented design is perfect for showcasing driver chip products, electrical equipment, technical specifications, and industrial applications. It enables semiconductor and electrical enterprises to present their products online and attract electronic and industrial customers. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Common Installation Questions for EyouCMS...
👁 38
(PC+WAP) Blue Spring Probe Manufacturer's Website – pbootCMS Template; Probe Charging Connector Website Source Code Download – 0577

(PC+WAP) Blue Spring Probe Manufacturer's Website – pbootCMS Template; Probe Charging Connector Website Source Code Download – 0577 Practical Collection pbootcms Template

A PbootCMS website template designed for manufacturers of spring needle and probe charging connectors, featuring a blue color scheme and supporting both PC and WAP devices. Its professional and sophisticated design is ideal for showcasing electronic connector products and core technologies. This template helps precision electronics companies build their online brand identity and expand their customer base within the electronics industry. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin...
👁 50
Responsive Metal Craft Pendant Website Template 0204

Responsive Metal Craft Pendant Website Template 0204 Practical Collection Yiyou template

An eYouCMS responsive website template designed specifically for the metal crafts and accessories industry. Its sophisticated and artistic design is perfect for showcasing metal craft products, accessories, creative designs, and brand stories. This template helps craft brands showcase their products online and attract both collectors and gift-giving customers. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Common Questions About eYouCMS Installation...
👁 42
A simple yet powerful HTML code snippet – iFearMe Framework

A simple yet powerful HTML code snippet – iFearMe Framework Summary of pitfalls Practical Collection Utility tools

Since I prefer automation when building websites, many tasks require multiple visits to a single URL to process content stored on the server. In such cases, for various reasons, it may not be possible to simply refresh the page directly; this is where the iFearMe framework comes in handy! The iFearMe framework code: <!DOCTYPE html> <html> <head> <title...
👁 212
Responsive Tech Smart Product Website Template 1028

Responsive Tech Smart Product Website Template 1028 Practical Collection Yiyou template

An EyouCMS responsive website template designed specifically for the smart technology products industry. Its cutting-edge design style effectively showcases smart products, technological R&D initiatives, application scenarios, and brand strength. This template helps technology companies present their innovative products online, attracting both investment and customers. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 43
(Adaptive mobile version) Brand Strategy & Design Website – PBootCMS Template; Download source code for high-end design firm websites – 0578

(Adaptive mobile version) Brand Strategy & Design Website – PBootCMS Template; Download source code for high-end design firm websites – 0578 Practical Collection pbootcms Template

A PBootCMS website template designed for brand strategy and high-end design projects, compatible with both PC and WAP devices. Its sophisticated design style is ideal for showcasing comprehensive brand solutions, creative designs, and business case studies. This template helps high-end design firms establish their brand image online and attract premium clients. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn | Related...
👁 52