473,785 Members | 2,819 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to convert span tags in a String?

9 New Member
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
6 3227
mangal
9 New Member
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 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. );
  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 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;

Regards,

Mangal Kumar
Oct 25 '10 #4
mangal
9 New Member
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 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. );
  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 New Member
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
1935
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
0
1291
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">
2
11945
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
8
4206
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...
3
28531
by: Wallace | last post by:
Hai, Can anyone tell how to convert an object into string? Please help me.... urgent.. Thanx in advance...
2
5247
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
4697
by: howa | last post by:
anyone heard that before?
1
4248
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
1
1705
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>
3
1921
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...
0
10324
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...
1
10090
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,...
0
8971
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
7499
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
6739
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
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4050
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2879
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.