我有一個字符串不可思議的綠巨人(2008年)和使用模式preg_replace函數的問題
/^\([0-9]{1,4}\)$/
刪除(2008)。 PHP代碼如下所示:
$x = trim(preg_replace("/^\([0-9]{1,4}\)$/", "", "The Incredible Hulk (2008)"));
,其結果是:
The Incredible Hulk (2008)
我在做什麼錯?
我有一個字符串不可思議的綠巨人(2008年)和使用模式preg_replace函數的問題
/^\([0-9]{1,4}\)$/
刪除(2008)。 PHP代碼如下所示:
$x = trim(preg_replace("/^\([0-9]{1,4}\)$/", "", "The Incredible Hulk (2008)"));
,其結果是:
The Incredible Hulk (2008)
我在做什麼錯?
您正在使用匹配起始行的^
字符。刪除它,它應該工作。
如果你也想擺脫空白的(
前的正則表達式變得/\s*\([0-9]{1,4}\)$/
取出「^」。
$x = trim(preg_replace("/\([0-9]{1,4}\)$/", "", "The Incredible Hulk (2008)"));
(2008)沒有固定在字符串的開頭。 「^」要求匹配從一行的開始處開始。
^
和$
是標記整個字符串的開始和結束。刪除兩個。
$x = trim(preg_replace("/\([0-9]{4}\)/", "", "The Incredible Hulk (2008)"));
只要刪除^
符號(行首)。
$x = trim(preg_replace("/\([0-9]{1,4}\)$/", "", "The Incredible Hulk (2008)"));
(你可能想要刪除$
標誌以及線(結束))對文檔中的PHP元字符
更多信息: http://www.php.net/manual/en/regexp.reference.meta.php
$result = preg_replace('/\([\d]{4}\)$/', '', 'The Incredible Hulk (2008)');