【问题标题】:How to extract the value in double quotation? [closed]如何提取双引号中的值? [关闭]
【发布时间】:2014-05-13 18:16:43
【问题描述】:

我需要提取定义为php变量的html标签的属性值:

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

srcposter的值应该分开并强制转换为数组

【问题讨论】:

  • 您要使用哪种语言?
  • 查看我的更新答案:)

标签: php split


【解决方案1】:

如果你的文本是一个php变量,你可以使用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);
?>

你也可以使用simplehtmldom

您可以为此使用 html 解析器。看到这个: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; 
}

【讨论】:

    猜你喜欢
    • 2012-07-07
    • 2021-08-27
    • 1970-01-01
    • 2021-02-07
    • 2010-09-08
    • 2016-03-14
    • 2014-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多