View Full Version : confused, preg_replace...

Coastal Web
06-09-2005, 07:09 AM
Hello, here is a snip from my phpBB and this is for the BBCode function that replaces [ img]url_here[ /img] with the users image..

I was trying to change this so it would allow URLS with ?'s in them...

for instance, as it stands now if l posted:
[ img]http://www.example.com/img.jpg?size=full[/ img]

The ? breaks the [ img] and it doesn't show up.
So basically l'm trying to make it so that the board will allow ?'s to be posted in [ img] URL's...

What would l need to change in the code bellow to make this happen?

// image_url_here code..
$text = preg_replace("#\[img\]((http|ftp|https|ftps)://)([^ \?&=\#\"\n\r\t<]*?(\.(jpg|jpeg|gif|png)))\[/img\]#sie", "'[img:$uid]\\1' . str_replace(' ', '%20', '\\3') . '[/img:$uid]'", $text);

I just dont understand the preg_replace mess ^that thing is...

thanks for the help :D

06-09-2005, 04:53 PM
Hi Samantha,
To respond to the ^ question, where its currently located its searching for NOT in. So, [^ \?&=\#\"\n\r\t<]*? searches for any character that is not a ? &=#"newline or tab, any number of times. The question mark there isn't nessessary, as its searching for zero or more occurances. This however, is not what your looking to adjus, you looking for after the extension part. Now, I'd need to through something together quickly for you, but ATM I'm not feeling well so I won't be able to add it for you, but after the extension check, you would probably add:


I could trim it down for you, but something of the sorts should work. Where its located should also drop it into your first backreference as well.

06-09-2005, 09:43 PM
I'm confused too. What exactly does preg_replace() do?

06-09-2005, 11:07 PM
preg_replace searches through a string that you pass it looking for matches that you specify with regular expressions. If it finds a match it replaces it with something else you've specified.

In this case the preg_replace() is looking for a url pointing to an image specified in a section of text between and tags and matching a specified format so that it can replace it with the proper format.

Coastal Web
06-10-2005, 12:54 AM
Sorry l still can't get it to work, here's what l've tried:

// image_url_here code..
$text = preg_replace("#\[img\]((http|ftp|https|ftps)://)([^ \?&=\#\"\n\r\t<]*?(\.(jpg|jpeg|gif|png))(\.(jpg|jpeg|gif|png))(\?([^\?&=\#\"\n\r\t<]*=[^\?&=\#\"\n\r\t<]*)*)?)\[/img\]#sie", "'[img:$uid]\\1' . str_replace(' ', '%20', '\\3') . '[/img:$uid]'", $text);

any ideas?

Samantha Gram

06-10-2005, 03:53 AM
The question mark there isn't nessessary, as its searching for zero or more occurances.

I disagree. The question mark, in this case, makes the expression "non-greedy"

Consider the following string:

<img src="/images/myImg.jpg" alt="this is my image" />

If I use this regular expression:

/img src=\"(.*)"/

I will match "/images/myImg.jpg" alt="this is my image"

On the other hand, if I use

/img src=\"(.*?)"/

I will match "/images/miImg.jpg"