Как насчет синтаксического анализа DOM с помощью PHP Simple HTML DOM Parser
Вы можете скачать скрипт здесь: http://sourceforge.net/projects/simplehtmldom/files/
Если вы загрузите этот скрипт в свой текущий скрипт, как это:
include_once("simple_html_dom.php");
А затем вы можете просмотреть все изображения в своем HTML-коде и делать с ними все, что хотите:
$html = "Your HTML code";
foreach($html->find('img') as $element) {
// Do something with the alt text
$alt_text = $element->alt;
// Replace the image
$element->src = 'new_src';
$element->alt = 'new_alt';
}
Без использования библиотеки:
// Load the HTML
$html = "Your HTML code";
$dom = new DOMDocument();
$dom->loadHTML($html);
// Loop through all images
$images = $dom->getElementsByTagName('img');
foreach ($images as $image) {
// Do something with the alt
$alt = $image->getAttribute('alt');
// Replace the image
$image->setAttribute("src", "new_src");
$image->setAttribute("alt", "new_alt");
}
// Get the new HTML string
$html = $dom->saveHTML();