【问题标题】:how to get values of image alt="..." in php?如何在 php 中获取图像 alt="..." 的值?
【发布时间】:2014-05-17 16:58:13
【问题描述】:

大家好,我得到了下面显示的多组数据(在 $code 变量中)。我想知道如何输出图像 alt="..." 的所有值?比如我想得到:Music Club (sun) Music 09

来自:

        <img src="http://www.example.com/teststorage/episodes/11224/201.jpg" alt="Music Club ( sun ) Music 09" />

样本数据集:

    <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12 item">
        <div class="portfolio-item">
            <a href="http://www.example.com/en/test/1234/11224/Music 09" title="Music Club ( sun ) Music 09" class="portfolio-item-link" >
                <span class="portfolio-item-hover"></span>
                <span class="fullscreen"><i class="icon-play"></i></span>
                <img src="http://www.example.com/teststorage/episodes/11224/201.jpg" alt="Music Club ( sun ) Music 09" />
            </a>
            <div class="portfolio-item-title">
                <a href="http://www.example.com/en/test/1234/11224/Music 09" title="Music Club ( sun ) Music 09" class="portfolio-item-link"><h4>Music 09</h4></a>
            </div>
            <div class="clearfix"></div>
        </div>
    </div>

【问题讨论】:

  • 你没有。您不使用正则表达式处理 html。您使用 DOM 和 xpath,例如//img/@alt

标签: php parsing preg-match-all alt


【解决方案1】:

如果你真的想使用preg_match_all,你可以使用下面的代码。

<?php
$str = <<<END
<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12 item">
        <div class="portfolio-item">
            <a href="http://www.example.com/en/test/1234/11224/Music 09" title="Music Club ( sun ) Music 09" class="portfolio-item-link" >
                <span class="portfolio-item-hover"></span>
                <span class="fullscreen"><i class="icon-play"></i></span>
                <img src="http://www.example.com/teststorage/episodes/11224/201.jpg" alt="Music Club ( sun ) Music 09" />
            </a>
            <div class="portfolio-item-title">
                <a href="http://www.example.com/en/test/1234/11224/Music 09" title="Music Club ( sun ) Music 09" class="portfolio-item-link"><h4>Music 09</h4></a>
            </div>
            <div class="clearfix"></div>
        </div>
    </div>
END;

preg_match_all('/<img(.*?)alt=\"(.*?)\"(.*?)>/si', $str, $out, PREG_SET_ORDER);

//see first output
var_dump($out);

/*
array(1) {
  [0]=>
  array(4) {
    [0]=>
    string(105) "<img src="http://www.example.com/teststorage/episodes/11224/201.jpg" alt="Music Club ( sun ) Music 09" />"
    [1]=>
    string(65) " src="http://www.example.com/teststorage/episodes/11224/201.jpg" "
    [2]=>
    string(27) "Music Club ( sun ) Music 09"
    [3]=>
    string(2) " /"
  }
}
*/

//clean array
$alt = array();

foreach($out as $val) {
  $alt[] =  $val[2];
}

//see cleaned output
var_dump($alt);
/*
array(1) {
  [0]=>
  string(27) "Music Club ( sun ) Music 09"
}
*/
?>

如果你想做正确的事,我会调查simple_html_dom。您可以执行以下操作:

<?php
// Create DOM from URL or file
$html = file_get_html('http://www.example.com/page_i_want_to_spider.php');

// Find all images
foreach($html->find('img') as $element)
       echo $element->alt . '<br>';
?>

【讨论】:

  • hugo desing 非常感谢你的预匹配所有工作但如何使用 var_dump($out) 数组值作为超链接标题?>
  • 真的,听听所有的建议,不要使用它!只需检查 simple_html_dom 或 casimire et hippolyte 的答案。
  • 在我填充alt[]的foreach中,你也可以使用$val[2]的回声来显示它
【解决方案2】:

如果你有下面这样的 html 代码,那么你可以这样做

$re = '/(alt)=("[^"]*")/'; 
$str = '<div class="col-lg-3 col-md-3 col-sm-6 col-xs-12 item">\n        <div class="portfolio-item">\n            <a href="http://www.example.com/en/test/1234/11224/Music 09" title="Music Club ( sun ) Music 09" class="portfolio-item-link" >\n                <span class="portfolio-item-hover"></span>\n                <span class="fullscreen"><i class="icon-play"></i></span>\n                <img src="http://www.example.com/teststorage/episodes/11224/201.jpg" alt="Music Club ( sun ) Music 09" />\n            </a>\n            <div class="portfolio-item-title">\n                <a href="http://www.example.com/en/test/1234/11224/Music 09" title="Music Club ( sun ) Music 09" class="portfolio-item-link"><h4>Music 09</h4></a>\n            </div>\n            <div class="clearfix"></div>\n        </div>\n    </div>'; 

preg_match_all($re, $str, $matches);

输出:

    [0] => Array
        (
            [0] => alt="Music Club ( sun ) Music 09"
        )

    [1] => Array
        (
            [0] => alt
        )

    [2] => Array
        (
            [0] => "Music Club ( sun ) Music 09"
        )

【讨论】:

  • 感谢您的回复。我试过你的解决方案没有打印出来!输入数据不像您的示例那样在一行中,而是在我的第一篇文章中显示的块中。你能告诉我怎么解决吗?
  • 打印你需要在最后做print_r($matches);
  • 我确实使用了 print_r($matches) 但没有输出!
  • @user1788736 你打算用alt 文本做什么?
  • 我正在使用超链接标题的 alt 值。我创建超链接并使用超链接标题中的 alt 值
【解决方案3】:

使用 DOMDocument:

$dom = new DOMDocument();
@$dom->loadHTML($html);

$imgs= $dom->getElementsByTagName('img');

foreach ($imgs as $img) {
    if ($img->hasAttribute('alt')) echo $img->getAttribute('alt') . '<br/>';
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-30
    • 2019-05-14
    • 1970-01-01
    相关资源
    最近更新 更多