If I can borrow a few minutes of an expert's time? 🙂
I'm trying to make a 'smart' bbCode translator. One that will detect the valid start/end tags in a multiline string and replace them respectfully. Working outward from the inmost sets of tags, a lone start/end tag would not be replaced.
Basically, this is what I need to happen:
Example #1:
Hello there! [size=24]This text is [size=7]small[/size] compared to this text![/size]
Replaced:
Hello there! <span style="font-size:24">This text is <span style="font-size:7">small</span> compared to this text!</span>
Example #2:
Now, while [size=9]this text should be smaller, [size=18]this text is bigger because there is no 2nd end tag after the 2nd start tag to complete the replacement.[/size]
Replaced:
Now, while [size=9]this text should be smaller, <span style="font-size:18">this text is bigger because there is no 2nd end tag after the 2nd start tag to complete the replacement.</span>
This is what happens when I use the regexp:
preg_replace("/[size\=(7|9|12|18|24)](.*)[\/size]/is","<span style='font-size:\1px'>\2</span>",$text);
Example #3:
Now, while [size=9]this text should be smaller, [size=18]this text is bigger because there is no 2nd end tag after the 2nd start tag to complete the replacement.[/size]
Replaced:
Now, while <span style="font-size:9">this text should be smaller, [size=18]this text is bigger because there is no 2nd end tag after the 2nd start tag to complete the replacement.</span>
Same regexp, except a question mark is added:
preg_replace("/[size\=(7|9|12|18|24)](.*?)[\/size]/is","<span style='font-size:\1px'>\2</span>",$text);
Example #4:
Hello there! [size=24]This text is [size=7]small[/size] compared to this text![/size]
Replaced:
Hello there! <span style="font-size:24">This text is [size=7]small</span> compared to this text![/size]
I can't find any information about this anywhere.
Any ideas?