Question

I need to extract the value of the attributes of a html tag that is defined as a php variable:

[video src="http://localhost/video.mp4" poster="http://localhost/thumb.jpg" preload="none"][/video]

values ​​of src and poster should be separated and cast to array

Was it helpful?

Solution

if your text is a php variable, you can use preg_match :

<?php
$text = '[video src="http://localhost/video.mp4" poster="http://localhost/thumb.jpg" preload="none"][/video]';
preg_match('/src=\"([^\"]+)\"/si', $text, $src);
preg_match('/poster=\"([^\"]+)\"/si', $text, $poster);
$array['src'] = $src[1];
$array['poster'] = $poster[1];
print_r($array);
?>

also you can use simplehtmldom

You can use an html parser for this. See this one: http://simplehtmldom.sourceforge.net/.

$html = str_get_html('<video src="http://localhost/video.mp4" poster="http://localhost/thumb.jpg" preload="none"></video>');
foreach($html->find('video') as $element)
{
    $array['src'] = $element->src; 
    $array['poster'] = $element->poster; 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top