Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

preg_replace problem

Status
Not open for further replies.

bentleykf

Programmer
Apr 29, 2002
59
0
0
AU
i have the following preg_replace script, which is a part of a bbcode replace script;

Code:
// Search for this...
$replacewhat = array(
    '/\[img][/img](.+?)\[\/img\]/i',
    '/\[img]width=([0-9[/img]+) height=([0-9]+)\s*\](.+?)\[\/img\]/i',
    '/\[img]width=([0-9[/img]+)\s*\](.+?)\[\/img\]/i',
    '/\[img]height=([0-9[/img]+) width=([0-9]+)\s*\](.+?)\[\/img\]/i',
    '/\[img]height=([0-9[/img]+)\s*\](.+?)\[\/img\]/i',
);

// ...and replace it with this
$replacewith = array(
    '<img src="\\1" alt="" />',
    '<img src="\\3" alt="" width="\\1" height="\\2" />',
    '<img src="\\2" alt="" width="\\1" />',
    '<img src="\\3" alt="" width="\\2" height="\\1" />',
    '<img src="\\2" alt="" height="\\1" />',
);

$news = preg_replace($replacewhat, $replacewith, $news);

but what i need to do instead is surround the image tag with a link tag, linking to the same src in the image, and then i need to change the src of the image tag to the link to the thumbnail. for example;

instead of this code:
Code:
<img src="[URL unfurl="true"]http://www.WHATEVER.com/WHATEVER/images/imagefile.jpg"[/URL] width="x" height="y">

i want this code:
Code:
<a href="[URL unfurl="true"]http://www.WHATEVER.com/WHATEVER/images/imagefile.jpg"><img[/URL] src="[URL unfurl="true"]http://www.WHATEVER.com/WHATEVER/images/mini/pre_imagefile.jpg"[/URL] width="80" height="80" border="0">

i guess a preg_replace function is probably not the right one to use, but i dont know how to do this, and a preg_replace function actually changes all the occurences of the expression. How do i do this?
 
I use this way in order to take the wanted data create and array with all the important data I need and then just loop through the array and echo exacly the way I wanted!


-> I replace the html tag before and after the wanted data like this ->
$DataPrint[1] = str_replace("<img src=\"\"", "[link]", $DataPrint[1]);

$DataPrint[1] = str_replace("\" width=\"x\" height=\"y\">", "[/url]", $DataPrint[1]);

So from this

<img src=" width="x" height="y">

you will have this
[link]http://www.WHATEVER.com/WHATEVER/images/imagefile.jpg[/url]

preg_match_all("#\[link\]((.)+)\[/link\]#", $DataPrint[1], $headline);

And there I create an array with all the links!

Than you just maniputalte the link and echo the way you want!


Its compicated but it's working!



Hope it helps!
 
thanks, but my main problem is getting the two different URL's from the same URL, i need to add "mini/pre_" into the URL.
 
Can you send me 2 real examples from the url you have to try to make you the script!

I believe its not big deal!
 
ok,this is the original code
Code:
<img src="[URL unfurl="true"]http://www.ahsonline.com.au/bentleykf/phpnews/images/10.JPG"[/URL] alt="" style="border:none;" />

and this is the code i want
Code:
<a href="[URL unfurl="true"]http://www.ahsonline.com.au/bentleykf/phpnews/images/10.JPG"><img[/URL] src="[URL unfurl="true"]http://www.ahsonline.com.au/bentleykf/phpnews/images/mini/pre_10.JPG"[/URL] width="80" height="80" alt="" style="border:none;" /></a>
 
no, that didn't work, eregi_replace does not replace the script at all.
 
hi,
it worked for me(i tried the pattern in Javascript)...

Known is handfull, Unknown is worldfull
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top