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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Regular expressions trouble

Status
Not open for further replies.

Vormav

Programmer
Jun 21, 2002
12
US
I never have been good with regular expressions, perl-compatible or otherwise. And I've been looking at this for awhile. So I'll just tell exactly what I need, and see if anyone can write the expression for it.

Basically, I need to make a replacement on all image tags within some [size] bb coding tags. So what this would call for is searching for all occurrences of [size= followed by any number of integer values, followed by ] followed by any number of anything that does not match [/size] followed by <img followed by any number of anything that does not match
[/size] followed by [/size]
That's what to search for. In the replacement, I need to put all of this together, only add some text after every occurrence of <img within the [size] tags.

What I'm really trying to do is make it so that, like text, you can resize images on the forum with the use of the [size] tags. I already have the javascript figured out for it, but I need to insert an onLoad command into every image tag within the [size] tags. For quite some time now, I thought I had it working perfectly. But just today, I found out that it starts to screw up when you have multiple sets of image tags running around in your posts.

Anyway, I hope someone else can figure this one out, because I sure can't. X_x
Thanks
 
Sure thing.

-----

Regular text
This is some large text.
<img src=&quot;image1.gif&quot;>
<img src=&quot;image2.gif&quot;>
More text

Random text
<img src=&quot;image3.gif&quot;>

<img src=&quot;image4.gif&quot;>
Text


-----
Should become:
-----

Regular text.
This is some large text.
<img onLoad=&quot;loadcomplete(this, '5');&quot; src=&quot;image1.gif&quot;>
<img onLoad=&quot;loadcomplete(this, '5');&quot; src=&quot;image2.gif&quot;>
More text

Random text
<img src=&quot;image3.gif&quot;>

<img onLoad=&quot;loadcomplete(this, '2');&quot; src=&quot;image4.gif&quot;>
Text


-----
 
Although you can probably do it with a single regular expression, don't make yourself crazy.

I recommend that you write some kind of stateful text modifier. Read the file in a line at a time. Whenever your code sees &quot;\[size=\d+\]&quot;, it increments a counter. Whenever it sees &quot;\[/size\]&quot;, it decrements the counter.

The code then uses a second regular expression in a replace function to insert the new text if and only if the counter is greater than zero. Want the best answers? Ask the best questions: TANSTAAFL!
 
The only problem I can see with that is that some lines could possibly contain multiple instances of the [size] tag. Or, perhaps it would start with some stuff that isn't supposed to be resized, and end on some that is to be resized. Or maybe I just don't fully understand what you're suggesting. :-/
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top