用一个替换所有出现的东西

How to replace all ocurrences of <p>[gallery ids=""]</p> if they exist with only one?

  $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)) {

  }

The $content should be asdfsdfdsafasdfaasfddsaf <p>[gallery ids=""]</p>

Simple solution using preg_replace function:

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

You are escaping way to many things, you need to find the matter with a quantifier, {1,}in this case: between 1 and unlimited times.

Also add a g global modifier in case you've got newlines in your content.

$content = preg_replace('/(<p>\[gallery ids=""\]<\/p>){1,}/g', '$1', $content);