本文介紹了刪除括號之間的文本 PHP的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
限時送ChatGPT賬號..
我只是想知道如何在 php 中刪除一組括號和括號之間的文本.
I'm just wondering how I could remove the text between a set of parentheses and the parentheses themselves in php.
示例:
ABC(測試 1)
我想刪除(Test1)只留下ABC
I would like it to delete (Test1) and only leave ABC
謝謝
推薦答案
$string = "ABC (Test1)";
echo preg_replace("/([^)]+)/","",$string); // 'ABC '
preg_replace
是一個基于 perl 的正則表達式替換例程.這個腳本的作用是匹配所有出現的左括號,后跟任意數量的字符不是右括號,再后跟右括號,然后刪除它們:
preg_replace
is a perl-based regular expression replace routine. What this script does is matches all occurrences of a opening parenthesis, followed by any number of characters not a closing parenthesis, and again followed by a closing parenthesis, and then deletes them:
正則表達式分解:
/ - opening delimiter (necessary for regular expressions, can be any character that doesn't appear in the regular expression
( - Match an opening parenthesis
[^)]+ - Match 1 or more character that is not a closing parenthesis
) - Match a closing parenthesis
/ - Closing delimiter
這篇關于刪除括號之間的文本 PHP的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!