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

I have another question... I am usi

Status
Not open for further replies.

buzzt

Programmer
Joined
Oct 17, 2002
Messages
171
Location
CA
I have another question... I am using the following code to add html tags to a text file I am including. Basically [a] becomes <b>a</b>, #a# becomes <u>a</u>, and so on...

$content = preg_replace(&quot;'\[(.*?[a-zA-Z1-9])\]'&quot;,&quot;<b>\$1</b>&quot;,$content);
$content = preg_replace(&quot;'#(.*?[a-zA-Z1-9])#'&quot;,&quot;<u>\$1</u>&quot;,$content);
$content = preg_replace(&quot;'%(.*?[a-zA-Z1-9])%'&quot;,&quot;<i>\$1</i>&quot;,$content);

The problem is that I cannot use combinations like [#a#] to get <b><u>a</u></b>.

How would I write this to allow combinations?
 
That's because you're not matching those tags...

replace your
(.*?[a-zA-Z1-9])

with
(.*?[a-zA-Z1-9]\>?)

and it should work, though I might also add you should be able to neaten up those regex's a bit more... what're your parameters for what can be contained in the regex's, as you have it right now, it's anything as long as it ends in a number which is 1-9 or a letter.. (hence the reason it won't match if you end in </u>)

-Rob
 
Basically anything enclosed by those particular characters (#,%,[]) should apply the corresponding HTML tags.

It still does not seem to work. It will only apply the innermost tags as HTML. Whatever is outside of them is printed literally. [#aaaa#] becomes [<u>aaaa</u>] instead of <b><u>aaaa</u></b>. Maybe this should somehow be written into a single function with some sort of array?
 
Oh duh... wasn't including your special characters... here's the quick'n dirty... if I can find a more optimal answer in the next bit I'll post that, otherwise I'll have to wait till monday.
Code:
$content = preg_replace(&quot;'\[(.*?[a-zA-Z1-9>#%])\]'&quot;,&quot;<b>\$1</b>&quot;,$content);
$content = preg_replace(&quot;'#(.*?[a-zA-Z1-9>#%])#'&quot;,&quot;<u>\$1</u>&quot;,$content);
$content = preg_replace(&quot;'%(.*?[a-zA-Z1-9>#%])%'&quot;,&quot;<i>\$1</i>&quot;,$content);
 
Why am I having so much trouble here... forget the above post... this is what I meant to say..

Code:
$content = preg_replace(&quot;'\[(.*?[a-zA-Z1-9>#%])\]'&quot;,&quot;<b>\$1</b>&quot;,$content);
$content = preg_replace(&quot;'#(.*?[a-zA-Z1-9>%\]])#'&quot;,&quot;<u>\$1</u>&quot;,$content);
$content = preg_replace(&quot;'%(.*?[a-zA-Z1-9>#\]])%'&quot;,&quot;<i>\$1</i>&quot;,$content);
 
Nice.

Thanks... works well.
 
Ok, here's what I consider &quot;the right answer&quot;. It'll be significantly harder to break, and in my opinion is much easier to maintain.

Code:
$content = preg_replace(&quot;'\[([^\]]*)\]'&quot;,&quot;<b>\$1</b>&quot;,$content);
$content = preg_replace(&quot;'#([^#]*)#'&quot;,&quot;<u>\$1</u>&quot;,$content);
$content = preg_replace(&quot;'%([^%]*)%'&quot;,&quot;<i>\$1</i>&quot;,$content);

-Rob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top