sanjeev
sanjeev

Reputation: 13

How can find custom tag inside text in php

I looking for PHP solution, I have some HTML content with some custom tag like

$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";

I want Output of just below mention text only :

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

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

$FWithReplaceWord will be youtube "v=" ID eED6VRcj1Rs

Need output

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

I am using

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

But its not working. Please help me out.

Upvotes: 1

Views: 1016

Answers (3)

Casimir et Hippolyte
Casimir et Hippolyte

Reputation: 89547

An example with preg_replace_callback (stronger approach):

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

With preg_replace only (but a more naive approach):

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

Upvotes: 0

G.Margaritis
G.Margaritis

Reputation: 182

If i were you,I would try something like this:

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

In this case, assumming that you have ONE occurence of the tags and the content, you will able to access them with $tag[1][0], $tag[2][0] and $tag[3][0] which in your example will have values:

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

EDIT: If you want to to match [vid] tags with classes in them(e.g[vid class="vid1"]), then you will need to change your regex to this:

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

Upvotes: 2

Jitesh Sojitra
Jitesh Sojitra

Reputation: 4043

Logic

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

?>

Output:

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

Note:

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

Upvotes: 5

Related Questions