簡體   English   中英

對於字符類,preg_match錯誤:編譯失敗:缺少終止]

[英]preg_match error: Compilation failed: missing terminating ] for character class

我有一系列.txt文件,我正在for循環中讀取。 我在[widget_]格式的一些文本文件中放置了一個令牌

因此,文本文件的全部內容可能是[widget_search]。 另一個文本文件可能包含內容[widget_recent-posts]。 其他人可能只有html格式的文本而根本沒有令牌。

在for循環中,我正在做一個preg_match來查看文本文件是否與內容匹配我的標記模式的文件。 如果匹配,我正在執行一些條件代碼。

但是,當我運行跟蹤測試以查看是否匹配時,我收到錯誤。

錯誤是:

警告:preg_match()[function.preg-match]:C = \\ xampplite \\ htdocs \\ test \\ wp-content \\ plugins \\ widget-test \\ widget-test.php中偏移量為8的字符類的編譯失敗:缺少終止]在227行

這是第227行的代碼:

if (preg_match("/[widget_/i",$widget_text)) {//do something}

你應該逃脫[角色:

if (preg_match("/\[widget_/i",$widget_text)) { // do something }

使用像這樣的preg_quote更清潔:

if (preg_match(preg_quote("/[widget_/i"),$widget_text)) {//do something}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM