Hongmu Notes
Home Language Notes Remove the content between two specified characters in PHP without using regular expressions.
Language Notes PHP

Remove the content between two specified characters in PHP without using regular expressions.

Remove the content between two specified characters in PHP without using regular expressions.

This is a PHP function used to remove the content within specified parentheses from a string. It accepts three parameters:

  • Input string to be processed.
  • $leftBracket: The left boundary character for parentheses.
  • $rightBracket: The right boundary character for parentheses.

The logic within the function is as follows:

First, proceed.strpos()Function to search for a stringFirst occurrence in the textleftBracketAnd the corresponding onerightBracketPosition.

Then, enter a loop where the system continuously searches for and replaces the content within parentheses until no new pair of parentheses can be found. In each iteration, the content within the parentheses is replaced with an empty string, and the system proceeds to locate the next pair of parentheses. If no new pair of parentheses is found, the loop terminates and the resulting string `$Text` is returned.

Finally, return the processed string.

By calling this function, you can easily remove the content within specified parentheses from a string and obtain the modified string.

PHP code

private function removeInnerBrackets($text, $leftBracket, $rightBracket) {
        // 查找第一个左括号和对应的右括号的位置
        $start = strpos($text, $leftBracket);
        $end = strpos($text, $rightBracket, $start);
    
        // 循环查找并替换括号内的内容
        while ($start !== false && $end !== false) {
            // 获取括号内的内容
            $content = substr($text, $start, $end - $start + 1);
    
            // 替换括号内的内容为空字符串
            $text = str_replace($content, '', $text);
    
            // 继续查找下一个括号对的位置
            $start = strpos($text, $leftBracket, $start);
            $end = strpos($text, $rightBracket, $start);
        }
      
        return $text;
    }

 

微信赞赏

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 Precision Electronics Technology Website Template 0292

Responsive Precision Electronics Technology Website Template 0292 Practical Collection Yiyou template

An eyouCMS responsive website template designed for the precision electronics industry. Its modern, tech-driven design effectively showcases electronic products, R&D capabilities, manufacturing expertise, and industry applications. This template helps precision electronics companies showcase their brands online and attract industry clients. Template Overview | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for YououCMS | YououCMS...
👁 37
(Adaptive mobile version) PBootCMS template for contract template websites – Download source code for contract template websites – 0806

(Adaptive mobile version) PBootCMS template for contract template websites – Download source code for contract template websites – 0806 Practical Collection pbootcms Template

A PbootCMS website template designed for contract samples and contract templates, compatible with both PC and WAP devices. Its clean and practical design makes it ideal for providing downloads and sharing of various contract templates and legal document samples. This template can help legal information websites or document-sharing platforms attract more user traffic. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www.4s5.cn...
👁 62
(Adaptive Mobile Version) Responsive Mechanical Valve Equipment Website – PBootCMS Template; Red Hardware and Machinery Website Source Code Download – 0153

(Adaptive Mobile Version) Responsive Mechanical Valve Equipment Website – PBootCMS Template; Red Hardware and Machinery Website Source Code Download – 0153 Practical Collection pbootcms Template

This adaptive mobile-responsive PbootCMS template for mechanical valve equipment features a red color scheme. Its robust and professional design is ideal for showcasing industrial products such as valves, pumps, and pipe fittings. It enables valve manufacturing companies to demonstrate their technical expertise and product range online and attract engineering and industrial clients. Template Display | Installation Instructions | Website Backend: /admin.php | Username: admin | Password: admin | Extraction Password: www....
👁 36
Read the first line of a file in Python.

Read the first line of a file in Python. Language Notes python

Here are several methods to read the first line of a file in Python: Method 1: Use `readline()` with `with open('filename.txt', 'r') as file: first_line = file.readline() print(first_line)`. Note: This method reads the first line of the file; when it encounters ` '\n' `, it is treated as...
👁 1954
Custom Holiday Gift Ordering Website Template 0976

Custom Holiday Gift Ordering Website Template 0976 Practical Collection Yiyou template

An EyouCMS website template designed for the holiday gift and custom gift industry. Its sophisticated, premium design is ideal for showcasing gift products, customisation services, brand stories, and holiday promotions. This template enables gift customization brands to showcase their products online and attract both corporate and individual customers. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS...
👁 56
Responsive Precision Instrument Manufacturing Website Template 0293

Responsive Precision Instrument Manufacturing Website Template 0293 Practical Collection Yiyou template

This EyouCMS responsive template is ideal for the precision instrument manufacturing industry. Its professional and sophisticated design enables effective presentation of instrument products, manufacturing processes, technical specifications, and scientific research applications. It helps precision instrument manufacturers showcase their products online and attract both research and industrial clients. Template Display | Installation Instructions | Website Backend: /login.php | Username: admin | Password: admin | Related Articles: Summary of Common Installation Issues for EyouCMS | EyouCMS...
👁 48