2013-02-23 91 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

怎么样一个正则表达式匹配从字符串开头到第一个&符号之间的任何东西?

/^[^\&]+/