Forum Moderators: coopster

Message Too Old, No Replies

Replacing html link and all parameters with my own link in a file

Replacing html link and all parameters with my own link in a file

         

remeshx

8:50 pm on Dec 24, 2008 (gmt 0)

10+ Year Member



hi every one. i have been searching internet for 3 days and do not find any answer , please help me if you know - thanks

Q: i have html string that comes from a html file. it has lots of tags and text but.what i wand to do is to change the image tags and their link to my own images and links. for example:

find images and their links like


<a ... href="someURL.jpg"...><img ... src="thumb_URL.jpg"></a>

OR

<a ... href="someURL.jpg"...>SOME TEXT</a>

give me the Link URL and Thumb URL , and iwill do some actions on them and replace the orginals including all parameters :


<a alt="my alt" href="my_Url.jpg"><img alt="my_alt" src="my_thumb.jpg"></a>

OR

<a alt="my alt" href="my_Url.jpg">My Text</a>

i know something about preg_replace but not professional and any pattern i made, not worked :(. i just could extract the URLs and do my changes on them but i couldn't replace them with perevious tags in orginal file.

any help would be appreciated.
thanks for any help.

remeshx

4:09 am on Dec 25, 2008 (gmt 0)

10+ Year Member



i could extract Urls in <a> tags with this code:

preg_match_all('/<a[\s]+[^>]*href\s*=\s*[\"\']?([^\'\" >]+)[\'\" >]/i',$html,$out,PREG_PATTERN_ORDER) ;

but for replacement i tried below codes but they did not work:


$myhtml = Preg_replace('¦<a[^>].*[^>]'. preg_quote($old_url) .'[^>].*>¦U','<a href="my_URL" alt="$title" >',$html);

OR

$myhtml = Preg_replace('/<a[\s]+[^>]*href\s*=\s*[\"\']?' . $Old_URL . '[\'\" >]/i',$html) ;