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 ?
Change yours two loops to:
//....
$i=0;
foreach($html->find('img') as $element) {
$element->src = "new src";
$element->alt = "test" . $i;
$i++;
}
//....
<?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);
$i=0;
foreach($html->find('img') as $element) {
$element->src = "new src";
$element->alt = "test".$i;
$i += 1;
}
echo $html;
?>