tags:

views:

115

answers:

2

hi! as a warning I have very limited experience with PHP, with just over 1 month of learning it.

I have a textform that when its submitted it goes through str_replaces on the allowed tags (eg [img]) to turn them into html tags.

What I want to do Is grab the URLS from the $string and add links to the original image and the url to the resized. What im basically asking, i guess, is how do i grab urls from a string? so that I can use them to add links to resized imgs.

<a href="linkToOriginal"><img src=""location/resize.php?file=', $string);`

what i do at the moment is just replace [img] with html img tag which is put right next to the url in the text field. $string being the large body of text.

 $imageOpen = str_replace('[img]', '<img src="http://location/resize.php?file=', $string);
   $imageClose = str_replace('[/img]', '"/></a>', $imageOpen);

If this isnt clear please tell me, as im pretty awful at explaining things!

+1  A: 

If that's what you want to do every time, you should just have 2 text fields: "Image Source" and "Link Location", or something to that effect. Then you don't have to do any parsing, which is notoriously difficult.

Zack
This is true but is not exactly what im after but thanks for the answer!
babyrats
+2  A: 

I think you could better use some regular expressions here.. str_replace is to limited for this matter. When you use regular expressions you can open and close the tag in one replace with the value you want. You could take a look at this for instance:

    // images
$string = preg_replace("_\[img](.*)\[/img\]_si", '<img src="$1" alt="Image" />', $string);
Bloeper
I am scared of regex, but i will try using this! thanks for the answer
babyrats