471,319 Members | 928 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

Bold, italics, underline str_replac'd using the same char for opening and closing tags

Hello,

I want to write a _very_ simple text parser that would replace a
string like:

"This is text with /italics/, *bold* and _underline_."

and generate automatically something like this:

"This is text with <i>italics</i>, <b>bold</b> and <span
class="underline">underline</span>."

I've found a lot of code snippets explaining how to do BBCode, but
BBCode has different opening and closing pseudo-tags. In my example,
the opening and closing tags are the same (either /, * or _). So I
need something slightly more intelligent that would count the number
of /, * or _ tags, see if that number is even or odd and decide
accordingly if it must replace the char with an opening or closing
tag. (I hope my wording makes sense. It's very clear in my head :-D)

How would you implement that? I reckon it can't be hard, but I just
can't figure how to do it right now!

Thanks for any insight!
Jul 17 '05 #1
3 3732
Jean-Fran?ois Lacrampe wrote:
Hello,

I want to write a _very_ simple text parser that would replace a
string like:

"This is text with /italics/, *bold* and _underline_."

and generate automatically something like this:

"This is text with <i>italics</i>, <b>bold</b> and <span
class="underline">underline</span>."


<?php
$str = "This is text with /italics/, *bold* and _underline_.";
$reg = array(
"!/(.+)/!U",
"/\*(.+)\*/U",
"/_(.+)_/U"
);
$replace = array(
"<i>$1</i>",
"<b>$1</b>",
"<u>$1</u>"
);
$str = preg_replace($reg, $replace, $str);
print $str;
?>
JW

Jul 17 '05 #2
"Janwillem Borleffs" <jw@jwscripts.com> wrote in message news:<42***********************@news.euronet.nl>.. .
<?php
$str = "This is text with /italics/, *bold* and _underline_.";
$reg = array(
"!/(.+)/!U",
"/\*(.+)\*/U",
"/_(.+)_/U"
);
$replace = array(
"<i>$1</i>",
"<b>$1</b>",
"<u>$1</u>"
);
$str = preg_replace($reg, $replace, $str);
print $str;
?>
JW


WOW those perl regex are powerful! Every now and then I tell me I
should dig deeper and really understand them (beyond the basic stuff I
already know). This is pure and deep beauty! Now I want to learn them
NOW!

(If you (or anyone else) have and URL for a good tutorial on them,
please share, it will be greatly appreciated!)

Thanks, thanks and thanks again.
JFLac
Jul 17 '05 #3
Jean-Fran?ois Lacrampe wrote:
(If you (or anyone else) have and URL for a good tutorial on them,
please share, it will be greatly appreciated!)


A good starting point would be:

http://www.php.net/manual/en/referen...ern.syntax.php

Don't forget to read the user contributions on this page, which include a
couple of useful links.
JW

Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by sindre hiåsen | last post: by
15 posts views Thread by Jeff North | last post: by
13 posts views Thread by Bob | last post: by
2 posts views Thread by Adam Honek | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.