Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.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
Html 正则表达式:标准iFrame的Wordpress短代码_Html_Regex_Wordpress - Fatal编程技术网

Html 正则表达式:标准iFrame的Wordpress短代码

Html 正则表达式:标准iFrame的Wordpress短代码,html,regex,wordpress,Html,Regex,Wordpress,由于通过HTTPS提供SoundCloud短代码插件时存在一些小错误,我想在我的Wordpress博客上运行Regex搜索和替换。一般来说,我对实现这一点的方法进行了分类,但我正在寻找一种为某些可能性准备的正则表达式模式 当我从以下短代码手动获取iframe嵌入代码时 [soundcloud url="http://api.soundcloud.com/tracks/58082404" params="auto_play=false&show_artwork=false&colo

由于通过HTTPS提供SoundCloud短代码插件时存在一些小错误,我想在我的Wordpress博客上运行Regex搜索和替换。一般来说,我对实现这一点的方法进行了分类,但我正在寻找一种为某些可能性准备的正则表达式模式

当我从以下短代码手动获取iframe嵌入代码时

[soundcloud url="http://api.soundcloud.com/tracks/58082404" params="auto_play=false&show_artwork=false&color=372f2d" width="100%" height="166" iframe="true" /]
…它会变成这样:

<iframe width="100%" height="400" scrolling="no" frameborder="no" src="https://w.soundcloud.com/player/?visual=true&url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F58082404&show_artwork=true"></iframe>
我正在寻找一个正则表达式模式,确保其中任何一个都能转换为iframe播放器的嵌入代码

<iframe width="WIDTH" height="HEIGHT" src="URL&PARAMS"></iframe>


有人能提供一个有效的正则表达式模式来实现这一点吗?

未测试,但请参见方法。复制了
do\u shortcode\u标记
do\u shortcode
以附加到新的
内容
过滤器上:

<?php
    // Priority "10", do_shortcode has "11" and we would like parse that BEFORE the shortcodes will be handled...
    add_filter('the_content', 'replaceHTTPS', 10);

    function replaceHTTPS($content) {
        global $shortcode_tags;

        // Check if the content has shortcodes
        if(strpos($content, '[') === false) {
            return $content;
        }

        if(empty($shortcode_tags) || !is_array($shortcode_tags)) {
            return $content;
        }

        // Get the Rexex to find shortcodes
        $pattern = get_shortcode_regex();

        // Replace the stuff
        return preg_replace_callback("/$pattern/s", 'replaceHTTPS_Callback', $content);
    }

    function replaceHTTPS_Callback($matches) {
        global $shortcode_tags;

        // allow [[foo]] syntax for escaping a tag
        if($matches[1] == '[' && $matches[6] == ']') {
            return substr($matches[0], 1, -1);
        }

        $tag    = $matches[2];
        $attr   = shortcode_parse_atts($matches[3]);

        // LETS Start your own logical experiment!
        // Check if the shortcode is "soundcloud"
        if($tag == 'soundcloud') {
            // check all entrys
            if(isset($attr) && count($attr) > 0) {
                foreach($attr AS $name => $value) {
                    // When Attribute "url"
                    if(strtolower($name) == 'url') {
                        // And when starts with "http://"
                        $test = 'http://';
                        if(substr($value, 0, strlen($test) == $test) {
                            // Than replace it,..
                            $attr[$name] = str_replace($test, 'https://', $value);
                        }
                    }
                }
            }
        }

        // enclosing tag - extra parameter
        if(isset($matches[5])) {
            return $matches[1] . call_user_func($shortcode_tags[$tag], $attr, $matches[5], $tag) . $matches[6];

        // self-closing tag
        } else {
            return $matches[1] . call_user_func($shortcode_tags[$tag], $attr, null,  $tag) . $matches[6];
        }
    }

看看源代码:,在这里您可以找到短代码的逻辑方法。您可以在自己的筛选器中使用这些方法(例如,
内容
)。最适合您的函数:
shortcode\u parse\u atts($text)
Wow,没想到会这样(用正则表达式模式解决)。非常感谢,我将在周末试驾!只是为了记录(因为Stackoverflow不允许我进行1个字符的编辑):第45行缺少一个结束括号。除此之外,这不起作用,也不能解决我的问题。我想我真的需要对我的内容进行搜索和替换。这似乎做的工作,但我仍然需要一个良好的正则表达式模式。
<?php
    // Priority "10", do_shortcode has "11" and we would like parse that BEFORE the shortcodes will be handled...
    add_filter('the_content', 'replaceHTTPS', 10);

    function replaceHTTPS($content) {
        global $shortcode_tags;

        // Check if the content has shortcodes
        if(strpos($content, '[') === false) {
            return $content;
        }

        if(empty($shortcode_tags) || !is_array($shortcode_tags)) {
            return $content;
        }

        // Get the Rexex to find shortcodes
        $pattern = get_shortcode_regex();

        // Replace the stuff
        return preg_replace_callback("/$pattern/s", 'replaceHTTPS_Callback', $content);
    }

    function replaceHTTPS_Callback($matches) {
        global $shortcode_tags;

        // allow [[foo]] syntax for escaping a tag
        if($matches[1] == '[' && $matches[6] == ']') {
            return substr($matches[0], 1, -1);
        }

        $tag    = $matches[2];
        $attr   = shortcode_parse_atts($matches[3]);

        // LETS Start your own logical experiment!
        // Check if the shortcode is "soundcloud"
        if($tag == 'soundcloud') {
            // check all entrys
            if(isset($attr) && count($attr) > 0) {
                foreach($attr AS $name => $value) {
                    // When Attribute "url"
                    if(strtolower($name) == 'url') {
                        // And when starts with "http://"
                        $test = 'http://';
                        if(substr($value, 0, strlen($test) == $test) {
                            // Than replace it,..
                            $attr[$name] = str_replace($test, 'https://', $value);
                        }
                    }
                }
            }
        }

        // enclosing tag - extra parameter
        if(isset($matches[5])) {
            return $matches[1] . call_user_func($shortcode_tags[$tag], $attr, $matches[5], $tag) . $matches[6];

        // self-closing tag
        } else {
            return $matches[1] . call_user_func($shortcode_tags[$tag], $attr, null,  $tag) . $matches[6];
        }
    }