473,397 Members | 1,961 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,397 software developers and data experts.

How to convert span tags in a String?

9
Hi,

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>
  3.  
OUTPUT
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:

Regards,

Mangal
Oct 25 '10 #1

✓ answered by kovik

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. );
  6.  
  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. }
  11.  
  12. echo $content;

6 3193
mangal
9
Hi, anybody know the regular expression for the post, please post, it will help me lot

Megards,

Mangal
Oct 25 '10 #2
kovik
1,044 Expert 1GB
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. );
  6.  
  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. }
  11.  
  12. echo $content;
Oct 25 '10 #3
mangal
9
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;

Regards,

Mangal Kumar
Oct 25 '10 #4
mangal
9
kovik,

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>';
  5.  
  6. $styles = array(
  7.   'italic' => 'i',
  8.   'bold' => 'b',
  9. );
  10.  
  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. }
  15.  
  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. }
  20.  
  21. echo $content;
If possible then suggest me how to add more conditions in the regular exp;

With Regards;

Mangal
Oct 25 '10 #5
kovik
1,044 Expert 1GB
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. );
  5.  
  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. }
  10.  
  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
mangal
9
Thank you kovik,

I will do more research and update you

Regards,

Mangal Kumar
Nov 3 '10 #7

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

Similar topics

9
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....
0
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...
2
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...
8
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,...
3
by: Wallace | last post by:
Hai, Can anyone tell how to convert an object into string? Please help me.... urgent.. Thanx in advance...
2
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>
2
by: howa | last post by:
anyone heard that before?
1
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...
1
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...
3
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...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
0
tracyyun
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...
0
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,...
0
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...

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.