【问题标题】:how to save as a variable in preg_replace the content selected by the regex pattern?如何将正则表达式模式选择的内容保存为 preg_replace 中的变量?
【发布时间】:2013-09-26 10:14:17
【问题描述】:

 $Content = preg_replace($regex, $replace, $Content);

如何设置变量 preg_replace 动作前后 $content 的区别?

编辑

在我的情况下,$content 是我认为的字符串,看起来像这样

    <span style="color: #888888;"><img class="wp-image-3164 alignleft" style="color: #333333; margin: 5px;" alt="Modif 2" src="http://blablabla" width="200" height="132" />Rejoignez-nous en<strong> Egypte</strong> et découvrez une des mers les plus chaudes au monde, avec une <strong>visibilité remarquable</strong> et une grande variété d'espèces aquatiques endémiques. Safaga (port Egyptien) ouvert sur la Mer Rouge, situé à 45 minutes de l’aéroport d’Hurghada, est réputé pour son atmosphère non polluée, ses dunes de sable noir et sa <strong>tranquillité</strong>.</span>

正则表达式从中删除一些图像

【问题讨论】:

  • 你处理的是字符串还是数组?另外,有什么区别 - 长度、字符范围、完整的单词/句子差异?
  • 示例输入和输出在这里会有所帮助。
  • @SmokeyPHP $content 是一个字符串
  • 如果只是想查看img标签是否被移除,只需将preg_replace的返回值保存到$newContent然后做if($Content == $newContent) { /* replacement did nothing */ }
  • 不,我想将新内容放在页面的其他位置

标签: php regex string preg-replace


【解决方案1】:

preg_replace_callback 允许您在 preg_replace 第二个参数中使用函数。在该函数中,您现在可以处理找到的匹配项。

$removed_imgs = array();
$content = '<span style="color: #888888;"><img class="wp-image-3164 alignleft" style="color: #333333; margin: 5px;" alt="Modif 2" src="http://blablabla" width="200" height="132" />Rejoignez-nous en<strong> Egypte</strong> et découvrez une des mers les plus chaudes au monde, avec une <strong>visibilité remarquable</strong> et une grande variété d\'espèces aquatiques endémiques. Safaga (port Egyptien) ouvert sur la Mer Rouge, situé à 45 minutes de l’aéroport d’Hurghada, est réputé pour son atmosphère non polluée, ses dunes de sable noir et sa <strong>tranquillité</strong>.</span>';
$content = preg_replace_callback('#(<img.+? />)#',function($r) {
    global $removed_imgs;
    $removed_imgs[] = $r[1];
    return '';
},$content);
//Can now loop through all $removed_imgs

所提供的正则表达式的所有匹配项将从原始字符串中删除,但也会添加到$removed_imgs 数组中。

【讨论】:

  • 这看起来很棒......我用 $removed_imgs[] 替换了 $removed[] ..对吗?问题是它适用于这个定义的内容,但不适用于 wordpress grr 后台的内容
  • gettype($content) 返回 null !?
  • @Matoeil 是的,抱歉在我写完代码后重命名了变量,错过了一个实例。
  • @Matoeil 只要字符串工作正常并且没有出错,我就不会太担心gettype - 这可能是一个编码问题。也许is_string($Content) 会返回一些不同的东西。另请注意,变量区分大小写,$content 不是$Content
  • 你是个天才,我设法让它工作。我的 $content 不对。在进行 preg_replace_callback 处理之前,我需要使用 wordpress 函数 $content=get_the_content() 拥有一个包含我的后台内容的正确字符串
【解决方案2】:

你大概可以使用:xdiff_string_diff函数:

$repl = preg_replace($regex, $replace, $Content);
$diff = xdiff_string_diff($Content, $repl);

【讨论】:

【解决方案3】:

您可以在 preg_replace 之前使用preg_match_all。 像这样的:

preg_match_all($regex,$Content,$matches);
$Content = preg_replace($regex, $replace, $Content);
$diff = $matches;

$matches 是数组。

【讨论】:

  • 那是我不明白的事情。我已经做到了, print_r($diff) 返回一个空数组
  • 正则表达式没问题,因为 preg_replace 完成了它的工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-09
  • 1970-01-01
  • 2017-12-27
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多