Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用一个替换某事物的所有出现_Php_Regex_Preg Match All - Fatal编程技术网

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);