How to replace <font> tags with <span> tags

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
emoedesign
Forum Newbie
Posts: 3
Joined: Mon Jan 26, 2009 11:28 am

How to replace <font> tags with <span> tags

Post by emoedesign »

Hi there,

I'm quite new to php and am trying to replace font tags with span tags or to delete font tags all together as they are not xhtml valid.

I'm using file_get_contents to display current weather from superpages.com. it comes in fine but the code it produces uses font tags and other old tags such as <br>, <u>,<b>. I'd like to make the output xhtml valid but just don't know how to get rid of those old tags I've tried str_replace and preg_replace without great success. The following code did convert two <font></font> lines to <span></span> but other font tags that were not at the start of a line did not change:

Code: Select all

 
<?php
    $content = file_get_contents("http://www.superpages.com/cities/console.pl?T=Augusta&S=ME&Z=MEZ021&W=n120x120"); 
    if ($content !== false) {           
    // do something with the content
             $content = preg_replace('/<font(.*?)>(.*?)<\/font>/i', '<span>$2</span>', $content); 
 
     echo $content; 
               
     }else {
     echo "error processing page"; // an error happened
     }
?>
 
Here is the output that the above produced - you can see all of the font tags still there:

Code: Select all

 
<DIV style="display:none">
<!-- SiteCatalyst code version: H.9.
Copyright 1997-2007 Omniture, Inc. More info available at
http://www.omniture.com -->
<script language="JavaScript" src="//www.superpages.com/inc/s_code.js"></script>
<script language="JavaScript">
/* You may give each page an identifying name, channel, and hierarchy on the next lines. */
s.channel="Weather Consoles"
s.hier1="City Pages,Weather Pages,Weather Consoles"
<!-- End SiteCatalyst code version: H.9. -->
<!-- SiteCatalyst code version: H.9.
s.pageName=document.title;
var s_code=s.t();if(s_code)document.write(s_code);
</script>
</DIV>
<table width="123" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><img src="http://img.superpages.com/images-yp/decor/images/spacer.gif" width=3></td>
<td colspan="2" valign="middle"><font face="Arial, Helvetica, sans-serif" color="#333333" size="1">
<span>Today: 1/26/09</span><br><img height=40 alt="Very cold" src="http://img.superpages.com/cities/weathericons/verycold.gif" width=40 align="right"><font color="#FF0033" face="Verdana, Arial, Helvetica, sans-serif">High
15&deg;<br>
<span>Low 0&deg;</span><br>
<span>Very cold</span></font></font></td>
</tr>
<tr>
<td colspan="4" height="20" align="center" bgcolor="#000066"><font face="Verdana, Arial, Helvetica, sans-serif" size="1" color="#FFFFFF"><a href="divination2.cgi?Z=MEZ021&T=Augusta&S=ME" target="_top"><font color="#FFFFFF"><u>Seven
Day Forecast</u></font></a></font></td>
</tr>
</table>
 
I would also like to change the <td> bgcolor to a style also.

Any guidance will be greatly appreciated. :)

Thanks,
Trish
User avatar
HCBen
Forum Commoner
Posts: 33
Joined: Thu Jun 22, 2006 3:15 pm
Location: Indiana

Re: How to replace <font> tags with <span> tags

Post by HCBen »

You're close. Try:

Code: Select all

preg_replace('#(<font[^>]*>)(.*?)(<\/font>)#is', '<span>$2</span>', $content);
-Ben
emoedesign
Forum Newbie
Posts: 3
Joined: Mon Jan 26, 2009 11:28 am

Re: How to replace <font> tags with <span> tags

Post by emoedesign »

Thanks very much Ben. That seemed to get me closer for sure. There are still some pesky font tags remaining -Looks like <span> tags were put around some of the <font> tags. Here is the code and the output it produced:
Code:

Code: Select all

 
<?php
    $content = file_get_contents("http://www.superpages.com/cities/console.pl?T=Augusta&S=ME&Z=MEZ021&W=n120x120"); 
    if ($content !== false) {           
    // do something with the content
    
     $content = preg_replace('#(<font[^>]*>)(.*?)(<\/font>)#is', '<span>$2</span>', $content);
     echo $content; 
               
     }else {
     echo "error processing page"; // an error happened
     }
?>
 
output (i've highlighted the font tags in red):

Code: Select all

 
<table width="123" border="0" cellspacing="0" cellpadding="0">
<tr>
<td><img src="http://img.superpages.com/images-yp/decor/images/spacer.gif" width=3></td>
<td colspan="2" valign="middle"><span>
[color=#FF0000]<font face="Verdana, Arial, Helvetica, sans-serif">[/color]Today: 1/27/09</span><br><img height=40 alt="Sunny" src="http://img.superpages.com/cities/weathericons/sunny.gif" width=40 align="right"><span>High
21&deg;<br>
[color=#FF0000]<font color="#0099FF">[/color]Low 2&deg;</span><br>
<span>Sunny</span>[color=#FF0000]</font></font>[/color]</td>
</tr>
<tr>
<td colspan="4" height="20" align="center" bgcolor="#000066"><span><a href="divination2.cgi?Z=MEZ021&T=Augusta&S=ME" target="_top"><[color=#FF0000]font color="#FFFFFF">[/color] <u>Seven
Day Forecast</u></span></a> [color=#FF0000]</font>[/color] </td>
</tr>
</table>
 
Thanks again for your help. :)
Trish
User avatar
HCBen
Forum Commoner
Posts: 33
Joined: Thu Jun 22, 2006 3:15 pm
Location: Indiana

Re: How to replace <font> tags with <span> tags

Post by HCBen »

Well, you may be better off just running two replace functions then:

Code: Select all

$content = preg_replace('#<font[^>]*>#i', '<span>', $content);
$content = str_ireplace('</font>','</span>',$content);
Compact example:

Code: Select all

$content = str_ireplace('</font>','</span>',preg_replace('#<font[^>]*>#i','<span>',$content));
-Ben
emoedesign
Forum Newbie
Posts: 3
Joined: Mon Jan 26, 2009 11:28 am

Re: How to replace <font> tags with <span> tags

Post by emoedesign »

That worked perfectly - thanks so much for taking the time to help Ben - really appreciate it. :)
Trish
Post Reply