473,883 Members | 1,643 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to convert span tags in a String?

9 New Member

I need of assistance to convert spans having style related bold, ital and underline & color into html bold, underline and/or italicize tags.

Sample Input String:
Expand|Select|Wrap|Line Numbers
  1. <p>In <span style="font-family:'serif', 'Caslon'; font-size:11pt; font-style:italic; color:#221E1F">The Crack in the Cosmic Egg</span> I recounted 
  2. how, at a gathering of dor mitory mates around a table, <span style="font-family:'serif', 'Caslon'; font-size:11pt; font-style:italic; color:#221E1F">I demonstrated that fire didnít have to burn me</span>. We all smoked back then and I used up a full pack of Pall Mall <span style="font-family:'serif', 'Caslon'; font-size:11pt; font-weight:bold; color:#221E1F">cigarettes</span> (long unfiltered furnaces) to demonstrate my assertion.</p>
Expand|Select|Wrap|Line Numbers
  1. <p>In <i>The Crack in the Cosmic Egg</i> I recounted 
  2. how, at a gathering of dor mitory mates around a table, <i>I demonstrated that fire didnít have to burn me</i>. We all smoked 
  3. back then and I used up a full pack of Pall Mall <b>cigarettes</b> (long unfiltered furnaces) to demonstrate my assertion.</p>
Thanks in advance:


Oct 25 '10 #1
6 3240
9 New Member
Hi, anybody know the regular expression for the post, please post, it will help me lot


Oct 25 '10 #2
1,044 Recognized Expert Top Contributor
You could do this in JavaScript or PHP. But, since you asked in PHP, I'll help you with that. Just use regex to find these and replace them. Note that this will NOT work if you have nested <span> tags. Dealing with nesting is a whole different monster.

To make the regex, you are looking for a <span> tag with a style attribute containing a "font-style" CSS rule.

Expand|Select|Wrap|Line Numbers
  1. <span[^>]+?style="[^"]*font-style:\s?style-goes-here[^"]*"[^>]*>(.*?)</span>
In this regex, "style-goes-here" would be replaced by the style that you are after. \1 in this regex will be the contents of the <span> tag. You can use preg_replace() to do the replacements.

Expand|Select|Wrap|Line Numbers
  1. $content = 'whatever HTML content you are altering';
  2. $styles = array(
  3.     'italic' => 'i',
  4.     'bold' => 'b',
  5. );
  7. foreach ($styles as $font_style => $desired_tag) {
  8.     $regex = '~<span[^>]+?style="[^"]*font-style:\s?' . $font_style . '[^"]*"[^>]*>(.*?)</span>~';
  9.     $content = preg_replace($regex, "<$desired_tag>$1</$desired_tag>", $content);
  10. }
  12. echo $content;
Oct 25 '10 #3
9 New Member
Thanks Kovik,

You really saved my lot of time, i already tried different regular expression but could not make correct one; as i was including nested <span> logic, may be that was the reason;

kovik as you were saying that nested span case is different approach; if you have the logic of nested span then please post that also;

Very-2 thank for your kind help;


Mangal Kumar
Oct 25 '10 #4
9 New Member

It is not working in case of font-weight:bold. I have added one more foreach to handle this;

can be make it one for each by adding one more condition in regular expression:

Expand|Select|Wrap|Line Numbers
  1. $content = '<body><p><span style="font-size:11pt; font-style:italic; color:#221E1F">Mangal.</span></p>
  2. <p><span style="font-size:11pt; font-weight:bold; color:#221E1F">Pankaj.</span></p>
  3. <p><span style="font-size:11pt; color:#221E1F">Gaurav.</span></p>
  4. <p><span style="font-size:11pt; font-style:italic; color:#221E1F">Manish.</span></p></body>';
  6. $styles = array(
  7.   'italic' => 'i',
  8.   'bold' => 'b',
  9. );
  11. foreach ($styles as $font_style => $desired_tag) {
  12.     $regex = '~<span[^>]+?style="[^"]*font-style:\s?' . $font_style . '[^"]*"[^>]*>(.*?)</span>~';
  13.      $content = preg_replace($regex, "<$desired_tag>$1</$desired_tag>", $content);
  14. }
  16. foreach ($styles as $font_style => $desired_tag) {
  17.     $regex = '~<span[^>]+?style="[^"]*font-weight:\s?' . $font_style . '[^"]*"[^>]*>(.*?)</span>~';
  18.     $content = preg_replace($regex, "<$desired_tag>$1</$desired_tag>", $content);
  19. }
  21. echo $content;
If possible then suggest me how to add more conditions in the regular exp;

With Regards;

Oct 25 '10 #5
1,044 Recognized Expert Top Contributor
Oh. For some reason, my mind was thinking that bold was a font-style, not a font-weight. We'd just change it to this:
Expand|Select|Wrap|Line Numbers
  1. $styles = array(
  2.   'font-style:\s?italic' => 'i',
  3.   'font-weight:\s?bold' => 'b',
  4. );
  6. foreach ($styles as $css_rule => $desired_tag) {
  7.     $regex = '~<span[^>]+?style="[^"]*' . $css_rule . '[^"]*"[^>]*>(.*?)</span>~';
  8.      $content = preg_replace($regex, "<$desired_tag>$1</$desired_tag>", $content);
  9. }
  11. echo $content;
You need to keep the "\s?" because it allows for there to be a space. Technically, it should be "\s*?" since CSS ignores whitespace, but but there's no need to go there.

In order to deal with nesting, I'd recommend tokenization. Do some research on it, as it can be a fairly broad topic.
Oct 26 '10 #6
9 New Member
Thank you kovik,

I will do more research and update you


Mangal Kumar
Nov 3 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

by: David Henderson | last post by:
Hi There... I'm struggling with a problem: I have a string (coming from a rich text editor) which contains a variety of span tags that need to be replaced with corresponding formatting tags. e.g. <span style="font-weight: bold;">line 2</span> needs to become
by: slberry | last post by:
I have a client sending me XML where there are formatting tags embedded in XML entities.... i.e. <p content="This is &lt;B&gt;bold, and &lt;I&gt;bold-italic&lt;/I&gt;&lt;/B&gt;"/> I need to convert the entity to the proper XSL-FO syntax. This is <fo:inline font-weight="bold">
by: Eric Cota | last post by:
I have a page which has multiple span tags, I would like a javascript function that can look at each of these span tags for me. Depending on the what the user is doing there could be a different number of span tags so I don't want to hard code them. Thanks Eric
by: moondaddy | last post by:
I need to convert a byte array to a string and pass it as a parameter in a URL and then convert it back to the original byte array. However, its getting scrambled in the conversion. In short, here's the code: ====================================== Dim textConverter As New ASCIIEncoding Dim sParam As String = "This is my cool param" Dim bytParam() As Byte 'load the byte array here...
by: Wallace | last post by:
Hai, Can anyone tell how to convert an object into string? Please help me.... urgent.. Thanx in advance...
by: shapper | last post by:
Hello, Is there any validation problem using a <por a <h1tags inside a <spantag as follows: <span><p>Something</p><span> or <span><h1>Something</h1><span>
by: howa | last post by:
anyone heard that before?
by: jprimo | last post by:
Hi Everyone, I was wondering if anyone knew how to disable to span tags that appear in the place of ASP.NET labels ( <asp:Label ) after it has been processed by the server. I have a style sheet that I am trying to apply to a web page and these span tags are causing problems. I am using C# for my codebehind file. Any help is appreciated. Thanks
by: Izhaki | last post by:
Hi, I'm creating a system where my XML includes HTML tags (<h1></h1>) in addition to other XML elements (<book></book>). I would like to render the HTML tags back to HTML using XSL. Considering I want to replace all headings, I could do for each heading level (i.e. repeat the following code for h2, h3, h4, h5, etc.): <xsl:template match="h1"> <h1><xsl:apply-templates/></h1> </xsl:template>
by: Michellevt | last post by:
Hi I am working on a project (for college) and wondered if anyone can help me with my problem. In the project we are not allowed to make use of any "style" attributes but "class" attributes instead. The following is the java script that i am using and i am having trouble in using a class instead of a style tag because simply replacing the style=\"position:absolute;\" tag with class=\"posiAbs\" where in the external css ".posiAbs...
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...
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,...
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...
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
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();...
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.