All pastes #398137 Raw Edit

preg_replace

public text v1 · immutable
·published 2007-03-16 23:48 UTC
rendered paste body
OK I'm parsing some bbcode with php in my forums website.
Here's the scenario...I have an input textarea for where the end user puts their message to be posted on the boards. This textarea has several javascript buttons above it for formatting the text and inserting various html elements.  I recently modified the img javascript button so that it dynamically creates a file input control right above the textarea and at the same time inserts [thumb]thumb1[/thumb] whereever they are in their post. Both the number of file input controls and the thumb variable inside the thumb tags increase as you hit the img javascript button multiple times.  So if I were to hit it twice I would get [thumb]thumb1[/thumb][thumb]thumb2[/thumb] along with 2 file input controls above my textarea (<input type="file" name="thumb1"><input type="file" name="thumb1">).  I then have code which will upload these files to a location on my server.

Now when I hit submit, the bbcode get's parsed and I am doing a preg_replace to replace all instances of [thumb]thumb1[/thumb] with new bbcode in the form of [url=index.php?name=Forums&file=view_pic&pic=whatever][imgx]images/news/whatever.jpg[/imgx][/url]

here is my preg_replace code:

$hometext = preg_replace('/\[thumb\]([a-z0-9\.-_]+)\[\/thumb\]/e', '"[url=index.php?name=Forums&file=view_pic&pic=" . str_replace(" ", "_", $_FILES["\\1"]["name"]) . "][imgx]images/news/" . str_replace(" ", "_", $_FILES["\\1"]["name"]) . "[/imgx][/url]"', $hometext);

The result is great, it work flawlessly and creates exactly what I'm looking for...little thumbnails and are a hyperlink to the full image.  The only time it DOESN'T work is if I have 2 thumbs right next to each other ie. [thumb]thumb1[/thumb][thumb]thumb2[/thumb] . When I try this, it parses only a portion of the first [thumb]thumb1[/thumb] and never even sees the second...

If I seperate them with something like this [thumb]thumb1[/thumb]&nbsp;[thumb]thumb2[/thumb] it works perfect.  So I think there is something wrong with my preg_replace code.  Any suggestions?