0

I've got a large number of webpages stored in an MySQL database.

Most of these pages contain at least one (and occasionally two) entries like this...

<a href="http://first-url-which-always-ends-with-a-slash/">
  <img src="http://second-different-url-which-always-ends-with.jpg" />
</a>

I'd like to just set up a little php loop to go through all the entires replacing the first url with a copy of the second url for that entry.

How can I use preg to:

  1. find the second url from the image tag
  2. replace the first url in the a tag, with a copy of the second url

Is this possible?

2
  • yes, it is! de.php.net/preg_replace Commented Aug 17, 2012 at 9:49
  • Thanks but the "how" bit just as important to me, as knowing that it is possible... Commented Aug 17, 2012 at 14:25

3 Answers 3

1

see this url

PHP preg match / replace?

see also:- http://php.net/manual/en/function.preg-replace.php

$qp = qp($html);
foreach ($qp->find("img") as $img) {
    $img->attr("title", $img->attr("alt"));
}
print $qp->writeHTML();

Though it might be feasible in this simple case to resort to an regex:

preg_replace('#(<img\s[^>]*)(\balt=)("[^"]+")#', '$1$2$3 title=$3', $h);

(It would make more sense to use preg_replace_callback to ensure no title= attribute is present yet.)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it took me a while there to work out what qp($html) was all about. I don't understand how the preg_replace can get the src url from the img tag into the a href tag?
0

You can do following :

$dom = new DOMDocument('1.0', 'UTF-8');
$dom->formatOutput = true;
$source = "<a href=\"http://first-url-which-always-ends-with-a-slash/\">
  <img src=\"http://second-different-url-which-always-ends-with.jpg\" />
</a>";
$dom->loadHTML($source);
$tags = $dom->getElementsByTagName('a');

foreach ($tags as $tag) {
  $atag = $tag->getAttribute('href');  
  $imgTag = $dom->getElementsByTagName('img');
  foreach ($imgTag as $img) {        
    $img->setAttribute('src', $atag);
    echo $img->getAttribute('src');
  }
}

1 Comment

OK now i've found out about the DOMDocument Class, i can see that might do the trick! Cheers
0

Thanks for the suggestions i can see how they are better than using Preg.

Even so i finally solved my own question like this...

$result = mysql_query($select);
while ($frow = mysql_fetch_array($result)) {
    $page_content = $frow['page_content'];

    preg_match("#<img\s+src\s*=\s*([\"']+http://[^\"']*\.jpg[\"']+)#i", $page_content, $matches1);
    print_r($matches1);
    $imageURL = $matches1[1] ; 

    preg_match("#<a\s+(?:[^\"'>]+|\"[^\"]*\"|'[^']*')*href\s*=\s(\"http://[^\"]+/\"|'http://[^']+/')#i", $page_content, $matches2);
    print_r( $matches2 );  
    $linkURL = $matches2[1] ;

    $finalpage=str_replace($linkURL, $imageURL, $page_content) ;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.