473,748 Members | 2,567 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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="underlin e">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 3948
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="underlin e">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($r eg, $replace, $str);
print $str;
?>
JW

Jul 17 '05 #2
"Janwillem Borleffs" <jw@jwscripts.c om> 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($r eg, $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
2784
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 list inside the text. Of course it is possible to add the list and remove all space between li tags, but this is not so readable to the writer. What I try to do is also to add nl only in the plain text and inside li
15
6046
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 use. http://www.interactivetools.com/products/htmlarea/ This only works with IE5.5+. What I need to do is to take this html formatted text and only display part of the text on a web page (much like a news article which shows only part of the...
8
5744
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 http-equiv=Content-Type content="text/html; charset=iso-8859-1"> <title>Untitled</title>
1
2179
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 work. It works if the tags are <input type=radio...>. This line is OK:
13
3984
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 textarea input field, then will insert html tags accordingly. So, the user writes up a document in word that contains instances of italics, bold, and underline. The user then copies the text of that document into my textarea field. I am trying...
2
2866
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 change the selected text to bold, italic or underlined without using the font dialog?
3
2030
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 saw that VStudio automatically changes my URL addresses into real links. Sure would be nice to be able to tag certain words as bold or italics..... ANY easy way to do this?
2
14949
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 drawstring command is as follows: Somewhere in my declarations I have: Private ra As System.Drawing.StringFormat
1
8526
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 but doesn't foreach (DataGridViewCell cell in grid.SelectedCells) { cell.Style.Font = new Font(cell.Style.Font, FontStyle.Bold); } This does change it to bold but takes out italics and underline.
0
8996
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8832
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9562
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9386
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8255
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6078
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4879
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2217
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.