【问题标题】:Can't get preg_replace to work无法让 preg_replace 工作
【发布时间】:2016-04-14 04:02:43
【问题描述】:

我希望我只是犯了一个简单的错误,您可以帮助纠正。不幸的是,我对 PHP 不是很有经验。

我正在尝试对一个字符串运行两个正则表达式。

您会注意到第一个尝试定位脚本和 iframe,然后在其周围包裹一个 div。

第二个,只是尝试用 HTTP 协议替换“//” URL - 我意识到这个可以作为 str_replace 完成,我在下面注释掉了。我测试了 str_replace 正在努力确保没有任何有趣的事情没有调用这个函数并且它工作正常。由于某种原因,preg_replace 基本上被忽略并且字符串没有改变。

我在这里遗漏了什么明显的东西吗?

我已经尝试了几个在线 preg_replace 工具,它们似乎是正确的。

function cleanseSpringboardEmbed($content)
{
    // run regex over the content to clean up the embed code from springboard and make compatible with IA.
    $patternWrapper = '/<script src="\/\/www.springboardplatform\.com\/js\/overlay"><\/script><iframe(.*)<\/iframe>/';

    $patternProtocol = '/<iframe src="\/\/cms.springboardplatform.com/';

    $holder = $content;

    $replacementWrapper = '<figure class="op-interactive">' . '$0' . '</figure>';
    $replacementProtocol = '<iframe src="http://cms.springboardplatform.com';

    //$holder = str_replace("//cms.springboardplatform.com","http://cms.springboardplatform.com", $holder);
    //$holder = str_replace("//www.springboardplatform.com","http://www.springboardplatform.com", $holder);

    preg_replace($patternWrapper, $replacementWrapper, $holder);
    preg_replace($patternProtocol, $replacementProtocol, $holder);
    return $holder;
}

这是一些输入的示例

<p>test<br />
<script src="//www.springboardplatform.com/js/overlay"></script><iframe id="crzy003_1621795" src="//cms.springboardplatform.com/embed_iframe/5365/video/1621795/crzy003/craziestsportsfights.com/10" width="600" height="400" frameborder="0" scrolling="no"></iframe><br />
test</p>

【问题讨论】:

    标签: php regex wordpress preg-replace


    【解决方案1】:

    您在执行preg_replace 后忘记分配修改后的持有人值。根据上面的手册页

    preg_replace() 如果主题参数是一个数组,则返回一个数组, 或其他字符串。

    如果找到匹配项,则返回新的主题,否则 如果发生错误,主题将原样返回或返回NULL

    所以你应该修改你的代码:

    <?php
    
    function cleanseSpringboardEmbed($content)
    {
        // run regex over the content to clean up the embed code from springboard and make compatible with IA.
        $patternWrapper = '/<script src="\/\/www.springboardplatform\.com\/js\/overlay"><\/script><iframe(.*)<\/iframe>/';
    
        $patternProtocol = '/<iframe src="\/\/cms.springboardplatform.com/';
    
        $holder = $content;
    
        $replacementWrapper = '<figure class="op-interactive">' . '$0' . '</figure>';
        $replacementProtocol = '<iframe src="http://cms.springboardplatform.com';
    
        //$holder = str_replace("//cms.springboardplatform.com","http://cms.springboardplatform.com", $holder);
        //$holder = str_replace("//www.springboardplatform.com","http://www.springboardplatform.com", $holder);
    
        $holder = preg_replace($patternWrapper, $replacementWrapper, $holder);
        $holder = preg_replace($patternProtocol, $replacementProtocol, $holder);
        return $holder;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-23
      • 2014-12-26
      • 2017-12-22
      • 2012-02-28
      相关资源
      最近更新 更多