473,387 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 3928
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: sindre hiåsen | last post by:
Hi, What I try to do is to search a text for html tags. Why. Because I want to use nl2br on the text, but since this command add br instead of all /n it is not a nice thing to add t.x. a html...
15
by: Jeff North | last post by:
Hi, I'm using a control called HTMLArea which allows a person to enter text and converts the format instructions to html tags. Most of my users know nothing about html so this is perfect for my...
8
by: Hostile17 | last post by:
Consider the following HTML. ---------- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd"> <html> <head> <meta...
1
by: Jenny | last post by:
Hi, Can I create an array of tags by assigning same name to these tags? For example, I have two <p> tags with the same name t1. But document.all.b.value=document.all.t.length does not...
13
by: Bob | last post by:
Hello: I could use some regular expression help. I'm trying to create a client-side javascript function that will test user input to determine the markup of text that has been copied into a...
2
by: Adam Honek | last post by:
Okay, Assinging the font and its style (bold, italic etc.) using the font dialog is easy such as: FontDialog1.ShowDialog() txtEmailBody.SelectionFont = FontDialog1.Font But how does one...
3
by: tvance929 | last post by:
Ok, I am creating a string to go into a RTB. Is there any code or tags I can create within the string so that I can Bolden or Italicize certain words? I would normally assume no way until I...
2
by: Siv | last post by:
Hi, I want to print using the Graphics.Drawstring method but instead of using either bold or underline I want to combine both so the printed text is bold and underline?? The way I send the...
1
by: gmcconville | last post by:
Im a little bit stuck on how to change a cell in a datagridview to bold without affecting any of the other attributes of the cell, such as underline, italic etc. This is what I thought would work...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.