473,233 Members | 1,984 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,233 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 3920
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...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.