我有以下字符串字符串的出現次數:替換對應於正則表達式
$a = "test1";
$b = "test 2";
$c = "test<3";
$d = "test&4";
我想通過一些字母,以取代「&」後面出現,並通過終止「;」。
輸出應該是:
$a = "test1";
$b = "test 2";
$c = "test 3";
$d = "test&4";
我如何能做到這一點用PHP?
我有以下字符串字符串的出現次數:替換對應於正則表達式
$a = "test1";
$b = "test 2";
$c = "test<3";
$d = "test&4";
我想通過一些字母,以取代「&」後面出現,並通過終止「;」。
輸出應該是:
$a = "test1";
$b = "test 2";
$c = "test 3";
$d = "test&4";
我如何能做到這一點用PHP?
使用此:
$x = preg_replace('/&[a-z]+;/', ' ', $b);
echo $x;
在這種特殊情況下,你並不需要一個正則表達式,很有可能你需要的是對HTML實體進行解碼,並可以與html_entity_decode()中完成,如:
$a = html_entity_decode("test1");
$b = html_entity_decode("test 2");
$c = html_entity_decode("test<3");
$d = html_entity_decode("test&4");
var_dump($a,$b,$c,$d);
的答案@ this.lau_是最好的,但如果你想在正則表達式,試試這個
(\&)([a-z]{1,4})(;)
該解決方案將返回'「TES t <3「,而OP需要」測試3「。 –