regex pb !

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
aegypius
Forum Newbie
Posts: 3
Joined: Tue Jun 04, 2002 9:47 am

regex pb !

Post by aegypius »

Hi !

I have a problem using regex expressions. I try to manage themes from my php code and gfx links are giving a headache !

here's a sample of what i try to do :

<[whatever] src="[whatever]" [whatever]> replaced by
<^^^^^^^ src="[new dir][same as above]" ^^^^^^^^>

and here's my code :

Code: Select all

$outfile = eregi_replace("<&#1111;:ascii:]*&#1111;src="]&#1111;:ascii:]*&#1111;"]&#1111;:ascii:]*>","<img src="".TEMPLATE_PATH."$this->theme/", $outfile);
it doesn't work !

Could You help me please !

I 'm getting mad :evil:

aegypius
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Code: Select all

<?php
$TEMPLATE_PATH = 'anotherdir/';
$html = file('input.html');
$search = '/<img (.*)src="(.*)"(.*)>/';
$replace = "<img \\1src="$TEMPLATE_PATH\\2"\\3>";
$html = preg_replace($search, $replace, $html);
foreach($html as $line)
	print($line);
?>
will replace the src of <img>-Elements
User avatar
aegypius
Forum Newbie
Posts: 3
Joined: Tue Jun 04, 2002 9:47 am

a more efficient way to do that

Post by aegypius »

Code: Select all

$outfile=eregi_replace("<(&#1111;^>]*|&#1111;:space:]*)src=('|")(&#1111;^<]*)('|")(&#1111;^<]*|&#1111;:space:]*)>","<\\1src=\\2".TEMPLATE_PATH."$this->theme/\\3\\4>",$outfile);
And now it works with <script src= and javascrits like img.src !

Thanks for your help !

aegypius
User avatar
aegypius
Forum Newbie
Posts: 3
Joined: Tue Jun 04, 2002 9:47 am

a new version

Post by aegypius »

Code: Select all

$outfile=eregi_replace("<(&#1111;^frame]+&#1111;^>]*|&#1111;:space:]*)src=('|")(&#1111;^http://]+&#1111;^ftp://]+&#1111;^<]*)('|")(&#1111;^<]*|&#1111;:space:]*)>","<\\1src=\\2".TEMPLATE_PATH."$this->theme/\\3\\4>",$outfile)
This version fix various pb with frame and absolute url !

Have Fun !

aegypius
Post Reply