user328146
user328146

Reputation:

Replace images with regular expressions

I want to find and do some operations on this string:

<img src="images/video.png" border="0" alt="60" />
  1. Find every occurence in context
  2. Retrieve the alt attribute (in this case 60) and work with this number
  3. Replace the whole image

I've been playing around with regular expressions but it obviously doesn't work yet:

    if (preg_match_all('<img src="images/video.png" border="0" alt="[^"]*">', $content, $regs)) {
    for($i=0;$i<count($regs[0]);$i++){

        echo $regs[0][$i] . "<br>";
        $id = preg_replace('alt="[^"]*"', "$1", $regs[0][$i]);
        echo "The id: " . $id . "<br>";

    }
}

Upvotes: 0

Views: 450

Answers (6)

betamax
betamax

Reputation: 14061

How about parsing the DOM using PHP Simple HTML DOM Parser

You can download the script from here: http://sourceforge.net/projects/simplehtmldom/files/

If you load that script in to your current script like this:

include_once("simple_html_dom.php");

And then you can loop through all images in your HTML and do what you want with them:

$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';

}

Without using a library:

// 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();

Upvotes: 2

aercolino
aercolino

Reputation: 2356

You should use this regex

<img src="images/video.png" border="0" alt="([^"]*)" />

But if you want to admit this input too

<img alt="60" src="images/video.png" border="0" />

and any other possible permutation, then it's better to match the image tag on its own, and then match the alt attribute on its contents.

Upvotes: 0

Jon
Jon

Reputation: 437854

[Expanding my comment into an answer]

Here's some sample code to get you started on PHP's DOM library:

$html = '...';
$doc = new DOMDocument();
$doc->loadHTML($html);
$xpath = new DOMXPath($doc);

// Build the XPath query (you can specify very complex criteria here)
$images = $xpath->query('//img[@src="images/video.png" and @border="0"]');

foreach($images as $image) {
    echo 'This image has alt = '.
         $image->attributes->getNamedItem('alt')->nodeValue.
         '<br />';
}

You can look at an XPath tutorial if you want to customize the query with more advanced logic.

Upvotes: 0

yankee
yankee

Reputation: 40870

php > $xml = new SimpleXmlElement('<img src="images/video.png" border="0" alt="60" />');
php > foreach($xml->xpath('//@alt') as $alt) echo "Id is: ",(string)$alt,"\n";
Id is: 60

Upvotes: 0

Edgar Velasquez Lim
Edgar Velasquez Lim

Reputation: 2446

Regex isn't the recommended way to do this since malformed html is notoriously hard to regex accurately. You want to look into DOMDocument: http://php.net/manual/en/class.domdocument.php

Other alternatives are discussed here:

Robust and Mature HTML Parser for PHP

Upvotes: 1

Alex Ackerman
Alex Ackerman

Reputation: 1341

You should use DOM to parse XML/HTML...

Upvotes: 1

Related Questions