如何在php中找到文本内的自定义标签

时间:2016-08-18 18:58:06

标签: php preg-match-all

我在寻找PHP解决方案,我有一些HTML内容和一些自定义标签,如

$html = "he approaches very silently towards him but at the last point of time, the man gets the hint and manages to escape from the attack. ,
[vid] youtubeURL/watch?time_continue=58&v=eED6VRcj1Rs [/vid] What happens in the latter is just breath-taking and comes with a reason why this video has gone viral in such short span of time";

我只想提出下面提到的文字:

[vid] youtubeURL/watch?time_continue=58&v=eED6VRcj1Rs [/vid]

   or
[vid] youtubeURL/watch?time_continue=58&v=eED6VRcj1Rs [Endvid]

$ FWithReplaceWord将是youtube“v =”ID eED6VRcj1Rs

需要输出

<div class='embed-responsive embed-responsive-16by9 vid1'><iframe class='embed-responsive-item' src='//www.youtube.com/embed/$FWithReplaceWord' allowfullscreen></iframe></div>

我正在使用

preg_match_all('~([vid](.*?)[/vid])~', $html, $matches);

但它不起作用。请帮帮我。

3 个答案:

答案 0 :(得分:3)

逻辑

<?php

$html = "he approaches very silently towards him but at the last point of time, the man gets the hint and manages to escape from the attack. , [vid] youtubeURL/watch?time_continue=58&v=eED6VRcj1Rs [/vid] What happens in the latter is just breath-taking and comes with a reason why this video has gone viral in such short span of time";

function getBetweenTwoStrings ($string, $start, $end) {
 $string = " ".$string;
 $ini = strpos($string, $start);
 if ($ini == 0) return "";
 $ini += strlen($start);
 $len = strpos($string, $end, $ini) - $ini;
 return substr($string, $ini, $len);
}

// Assuming you need in array as well
$tag[0] = "[vid]";
$tag[1] = getBetweenTwoStrings($html, "[vid]", "[/vid]");
$tag[2] = "[/vid]";

echo $tag[0];
echo $tag[1];
echo $tag[2];

?>

输出:

[vid] youtubeURL/watch?time_continue=58&v=eED6VRcj1Rs [/vid]

注意:

Now you can play around the way you want the output.

答案 1 :(得分:2)

如果我是你,我会尝试这样的事情:

preg_match_all("/(\[vid\])(.*?)(\[\/vid\])/s", $html, $tag);

在这种情况下,假设您有一个标签和内容,您可以使用$ tag [1] [0],$ tag [2] [0]和$ tag [3]访问它们[0]在您的示例中将具有值:

$tag[1][0] = "[vid]";
$tag[2][0] = "youtubeURL/watch?time_continue=58&v=eED6VRcj1Rs ";
$tag[3][0] = "[/vid]";

修改 如果你想将[vid]标签与其中的类匹配(例如[vid class =“vid1”]),那么你需要将你的正则表达式改为:

preg_match_all("/(\[vid.*\])(.*?)(\[\/vid\])/s", $html, $tag);

答案 2 :(得分:0)

preg_replace_callback示例(更强的方法):

$str = <<<'EOD'
[vid] youtubeURL/watch?time_continue=58&v=eED6VRcj1Rs [/vid]
   or
[vid] youtubeURL/watch?time_continue=58&v=eED6VRcj1Rs [Endvid]
EOD;

$format = '<div class="embed-responsive embed-responsive-16by9 vid1"><iframe class="embed-responsive-item" src="//www.youtube.com/embed/%s" allowfullscreen></iframe></div>';

$result = preg_replace_callback('~\Q[vid]\E \s* ([^[\s]+) \s* \Q[/vid]\E~x',
    function ($m) use ($format) { 
        foreach (explode('&', parse_url($m[1], PHP_URL_QUERY)) as $param) {
             list($key, $value) = sscanf($param, '%[^=]=%s');
             if ($key == 'v') return sprintf($format, $value);
        };
        return $m[0];
    }, $str);

echo $result;

仅使用preg_replace(但更简单的方法):

$str = <<<'EOD'
[vid] youtubeURL/watch?time_continue=58&v=eED6VRcj1Rs [/vid]
   or
[vid] youtubeURL/watch?time_continue=58&v=eED6VRcj1Rs [Endvid]
EOD;

$replacement = '<div class="embed-responsive embed-responsive-16by9 vid1"><iframe class="embed-responsive-item" src="//www.youtube.com/embed/$1" allowfullscreen></iframe></div>';

$result = preg_replace('~\Q[vid]\E \s* [^[\s]+ [?&] v=([^[&\s]+) \s* \Q[/vid]\E~x', $replacement, $str);

echo $result;