Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/253.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 Can';I don’我不能让preg_替换工作_Php_Regex_Wordpress_Preg Replace - Fatal编程技术网

Php Can';I don’我不能让preg_替换工作

Php Can';I don’我不能让preg_替换工作,php,regex,wordpress,preg-replace,Php,Regex,Wordpress,Preg Replace,我希望我刚刚犯了一个简单的错误,你可以帮我改正。不幸的是,我对PHP不是很有经验 我试图在一个字符串上运行两个正则表达式 您会注意到第一个正在尝试定位脚本和iframe,然后在其周围环绕一个div 第二个,只是尝试用HTTP协议替换“/”URL——我意识到这一个可以作为str_替换来完成,我在下面对此进行了注释。我测试了str_replace的工作,以确保没有调用此函数不会有任何有趣的事情,并且它工作正常。出于某种原因,preg_replace基本上被忽略,而字符串没有改变 我是不是漏掉了什么明

我希望我刚刚犯了一个简单的错误,你可以帮我改正。不幸的是,我对PHP不是很有经验

我试图在一个字符串上运行两个正则表达式

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

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

我是不是漏掉了什么明显的东西

我尝试了一些在线preg_替换工具,它们似乎是正确的

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;
}
函数清除跳板嵌入($content)
{
//在内容上运行regex以清除springboard中的嵌入代码,并使其与IA兼容。

$patternWrapper='/执行后,您忘记分配修改后的holder值。根据上面的手册页

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

<?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;
}