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: - <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
-
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>
-
OUTPUT - <p>In <i>The Crack in the Cosmic Egg</i> I recounted
-
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
-
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
6 3227
Hi, anybody know the regular expression for the post, please post, it will help me lot
Megards,
Mangal
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. - <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. - $content = 'whatever HTML content you are altering';
-
$styles = array(
-
'italic' => 'i',
-
'bold' => 'b',
-
);
-
-
foreach ($styles as $font_style => $desired_tag) {
-
$regex = '~<span[^>]+?style="[^"]*font-style:\s?' . $font_style . '[^"]*"[^>]*>(.*?)</span>~';
-
$content = preg_replace($regex, "<$desired_tag>$1</$desired_tag>", $content);
-
}
-
-
echo $content;
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
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: - $content = '<body><p><span style="font-size:11pt; font-style:italic; color:#221E1F">Mangal.</span></p>
-
<p><span style="font-size:11pt; font-weight:bold; color:#221E1F">Pankaj.</span></p>
-
<p><span style="font-size:11pt; color:#221E1F">Gaurav.</span></p>
-
<p><span style="font-size:11pt; font-style:italic; color:#221E1F">Manish.</span></p></body>';
-
-
$styles = array(
-
'italic' => 'i',
-
'bold' => 'b',
-
);
-
-
foreach ($styles as $font_style => $desired_tag) {
-
$regex = '~<span[^>]+?style="[^"]*font-style:\s?' . $font_style . '[^"]*"[^>]*>(.*?)</span>~';
-
$content = preg_replace($regex, "<$desired_tag>$1</$desired_tag>", $content);
-
}
-
-
foreach ($styles as $font_style => $desired_tag) {
-
$regex = '~<span[^>]+?style="[^"]*font-weight:\s?' . $font_style . '[^"]*"[^>]*>(.*?)</span>~';
-
$content = preg_replace($regex, "<$desired_tag>$1</$desired_tag>", $content);
-
}
-
-
echo $content;
If possible then suggest me how to add more conditions in the regular exp;
With Regards;
Mangal
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: - $styles = array(
-
'font-style:\s?italic' => 'i',
-
'font-weight:\s?bold' => 'b',
-
);
-
-
foreach ($styles as $css_rule => $desired_tag) {
-
$regex = '~<span[^>]+?style="[^"]*' . $css_rule . '[^"]*"[^>]*>(.*?)</span>~';
-
$content = preg_replace($regex, "<$desired_tag>$1</$desired_tag>", $content);
-
}
-
-
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.
Thank you kovik,
I will do more research and update you
Regards,
Mangal Kumar
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 <B>bold, and
<I>bold-italic</I></B>"/>
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: 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...
| |
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: 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...
|
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...
|
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: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |