drep94225 2011-12-16 07:09
浏览 27
已采纳

在图片标签中替换alt时出错?

I have a test code using simple_dom_html:

<?php
include 'simple_html_dom.php';
$content = '<img src="name1.jpg" alt="name"><img src="name2.jpg" alt="name2">';
$html = new simple_html_dom();
$html->load($content);
$array_alt = array();
foreach($html->find('img') as $element) {
    $element->src = "new src";
    $array_alt[] = $element->alt;
}
for($i=0; $i<count($array_alt); $i++) {
    $array_alt[$i] = "test" . $i;
}
echo $html;

OUTPUT:

$html= '<img src="new src" **alt="name"**><img src="newsrc" **alt="name2"**>';

Error when echo $content, alt not change from name "name1", "name2" to "test1", "test2", how to fix it ?

  • 写回答

2条回答 默认 最新

  • dongwen7187 2011-12-16 07:14
    关注

    Change yours two loops to:

    //....
    $i=0;
    foreach($html->find('img') as $element) {
        $element->src = "new src";
        $element->alt = "test" . $i;
        $i++;
    }
    //....
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效