Php 用一个替换某事物的所有出现
如果所有的Php 用一个替换某事物的所有出现,php,regex,preg-match-all,Php,Regex,Preg Match All,如果所有的[gallery ID=”“]仅存在一个,如何替换它们 $string = "/\<p\>\[gallery ids=\"\"\]\<\/p\>/"; $content = "asdfsdfdsafasdfaasfddsaf <p>[gallery ids=""]</p><p>[gallery ids=""]</p><p>[gallery ids=""]</p>"; if (pr
[gallery ID=”“]
仅存在一个,如何替换它们
$string = "/\<p\>\[gallery ids=\"\"\]\<\/p\>/";
$content = "asdfsdfdsafasdfaasfddsaf <p>[gallery ids=""]</p><p>[gallery ids=""]</p><p>[gallery ids=""]</p>";
if (preg_match_all($string, $content, $matches)) {
}
$string=“/\\[画廊ID=\”\“\]\/”;
$content=“asdfsdsafasdfaasfddsaf[gallery ID=”“][gallery ID=”“][gallery ID=”“]”;
if(preg_match_all($string,$content,$matches)){
}
$content
应该是asdfsdsafasdfaasfddsaf[gallery id=”“]
使用preg\u replace
功能的简单解决方案:
$content = 'asdfsdfdsafasdfaasfddsaf <p>[gallery ids=""]</p><p>[gallery ids=""]</p><p>[gallery ids=""]</p>';
$content = preg_replace("/(<p>\[gallery ids=\"\"\]<\/p>){2,}/", "$1", $content);
print_r($content);
$content='asdfsddsafasdfaasfddsaf[gallery ids=”“][gallery ids=“”][gallery ids=“”];
$content=preg\u replace(“/(\[gallery id=\”\“\”\”){2,}/”,“$1”,$content);
打印(内容);
你在逃避很多事情,你需要用一个量词,{1,}
在这种情况下:在1到无限次之间
如果内容中有换行符,还可以添加一个g
global修饰符
$content = preg_replace('/(<p>\[gallery ids=""\]<\/p>){1,}/g', '$1', $content);
$content=preg\u replace('/(\[gallery id=”“\]){1,}/g','$1',$content);