2013-02-23 52 views
0

我不擅長寫php正則表達式,現在有些數據像searchword&num=10我想用preg替換刪除&num=10或有時候可能是&num=10,謝謝。php preg replace&num =和& num =

echo preg_replace(array('/\&num=(d+)/i','/(&)num=(d+)/i'),'','searchword&num=10'); 
//I would like to only get searchword 

回答

1

您可以在這裏使用html_entity_decode

$searchStr = html_entity_decode($searchStr); 
echo preg_replace('/\&num=\d+/i', '', '$searchStr); 

雖然,你可以簡單地使用SUBSTR這樣

echo substr($searchStr, strpos('&')); 
+0

我喜歡第一個解決方案,而不是'strpos'或'/^[^ \&] + /',一些搜索詞來包含'&',應該避免殺死它們。 – 2013-02-23 11:44:43

+0

如果這是一種可能性,那麼html_entity_decode將適合您。 – Achrome 2013-02-23 11:46:54

1

怎麼樣一個正則表達式匹配從字符串開頭到第一個&符號之間的任何東西?

/^[^\&]+/