如何在PHP中修复“ preg_replace”函数

问题描述 投票:0回答:1

我在PHP“ preg_match”中具有某些功能,但是我不知道它仍然无法正常工作。我想使用正则表达式创建简码,例如“某些文本[youtube id = asd123]”将替换为“某些文本”。

如何解决此问题,没有结果,没有错误,仍然是空白的白色屏幕。请帮助,我将在8个小时后使用此功能。

*我的错误,只需将/ g替换为/ is,但是,如何在没有id的情况下获得$ 1 =

$post = new Post("Tutorial membuat CRUD dengan Laravel: [youtube id=HmV4gXIkP6k]");
echo $post->getHtmlContent();

class Post
{
protected $content;

/**
 * Post constructor.
 */
public function __construct($content)
{
    $this->content = $content;
}

public function getHtmlContent()
{
    $searchReplace = new SearchReplace;

    if( preg_match( $searchReplace->regex(), $this->content ) )
    {
        return preg_replace($searchReplace->regex(), $searchReplace->replace(), $this->content);
    }
    else
    {
        return preg_replace($searchReplace->regex(), $searchReplace->replace(), $this->content);
    }
}

}

class SearchReplace
{
public function regex()
{
    $search = array (
       // '/\[youtube(\s.*?)?\](?:([^\[]+)?\[\/youtube\])?/g',
       // '/\[YOUTUBE(\s.*?)?\](?:([^\[]+)?\[\/YOUTUBE\])?/g',
       // '/\[gist(\s.*?)?\](?:([^\[]+)?\[\/gist\])?/g',
       // '/\[GIST(\s.*?)?\](?:([^\[]+)?\[\/GIST\])?/g',
        '/\[youtube(\s.*?)?\](?:([^\[]+)?\[\/youtube\])?/is',
        '/\[YOUTUBE(\s.*?)?\](?:([^\[]+)?\[\/YOUTUBE\])?/is',
        '/\[gist(\s.*?)?\](?:([^\[]+)?\[\/gist\])?/is',
        '/\[GIST(\s.*?)?\](?:([^\[]+)?\[\/GIST\])?/is',
    );

    return $search;
}

public function replace()
{
    $replace = array (
        '<iframe width="500" height="315" src="https://www.youtube.com/embed/$1 frameborder="0" allowfullscreen></iframe>',
        '<iframe width="500" height="315" src="https://www.youtube.com/embed/$1 frameborder="0" allowfullscreen></iframe>',
        '<script src="https://gist.github.com/you-think-you-are-special/$1.js"></script>',
        '<script src="https://gist.github.com/you-think-you-are-special/$1.js"></script>',
    );

    return $replace;
}
}
php preg-replace preg-match bbcode
1个回答
0
投票

我的错误,只需将/ g替换为/ is但是,如何在没有id的情况下获得$ 1 =

© www.soinside.com 2019 - 2024. All rights reserved.