Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] match <font> tags attributes ?


[SOLVED] match <font> tags attributes ?

Reply
  #1 (permalink)  
Old 06-26-08, 07:51 AM
Mythvn Mythvn is offline
Wannabe Coder
 
Join Date: Jan 2006
Posts: 111
Thanks: 2
Thanked 0 Times in 0 Posts
[SOLVED] match <font> tags attributes ?

i want to search for all the <font> tags in my page and replace it with the <span> tags with i need to capture their attributes too ( face, size, color ...) so that i can apply certain style sheets to the new tags. can anybody help me please ? thanks

HTML Code:
<font face="Arial" size="10">Text</font>
<font face='Arial' size=10>Text</font>
<font face=Arial size='10'>Text</font>
<font face="Arial" size=10>Text</font>
Reply With Quote
  #2 (permalink)  
Old 06-26-08, 12:36 PM
aditya2071990 aditya2071990 is offline
Newbie Coder
 
Join Date: Jun 2008
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Use a good word processor or a web editor like dreamweaver! Then all you need to do is use the find and replace function all over the page, DUH!
Reply With Quote
  #3 (permalink)  
Old 06-26-08, 02:27 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
If you're on a 'nix box - use sed.

This command extracts everything with #xxxxxx and replaces it with color:#xxxxxx

Code:
grep -hr "[:|\ ]#[0-9a-fA-F]" *.css | sed "s/[^#]*\([^\ |^\"|^\;|^\!]*\)\(.*\)/color:\1/" > raw.colors
sort raw.colors | uniq
You can see a use of it in the color mapper - which may be helpful.

http://wirehopper.com/images/color_mapper.php
Reply With Quote
  #4 (permalink)  
Old 06-26-08, 03:59 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
Quote:
Originally Posted by aditya2071990 View Post
Use a good word processor or a web editor like dreamweaver! Then all you need to do is use the find and replace function all over the page, DUH!
For just about every post in this forum, there are several valid solutions.

Every post is either someone looking for an answer or offering help.

Although you may consider find and replace an obvious solution, it is not the only solution, and it may not be the best.

Offer your assistance with respect.
Reply With Quote
  #5 (permalink)  
Old 06-27-08, 01:47 PM
aditya2071990 aditya2071990 is offline
Newbie Coder
 
Join Date: Jun 2008
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Talking

Quote:
Originally Posted by wirehopper View Post
For just about every post in this forum, there are several valid solutions.

Every post is either someone looking for an answer or offering help.
That is obvious, baby, this is a forum, and that's what people do at forums, its great that you discovered that fact at least now.

Quote:
Although you may consider find and replace an obvious solution, it is not the only solution, and it may not be the best.
The OP did not mention the fact that he was trying to replace stuff entered in text-fields by users. The way he said it, it would look like he was trying to replace bits of code in his own html pages he was trying to make, unless one had tentacles that can detect what the other person is thinking. OP must have made sure that everyone understands his question with ease, and must have mentioned what exactly it is that he is trying to do.

Quote:
Offer your assistance with respect.
This is a forum, not a court room. Most of us are here coz we like helping people and having fun on the way, and not to criticize how others are answering. I have not crossed general boundaries of 'respect' by indicating that the solution was obvious, and there's no need for you to feel so stuffed about it.

Cool down, mate, and have a nice day!
Reply With Quote
  #6 (permalink)  
Old 06-28-08, 11:37 AM
Mythvn Mythvn is offline
Wannabe Coder
 
Join Date: Jan 2006
Posts: 111
Thanks: 2
Thanked 0 Times in 0 Posts
Thanks so much for your answers but I'm looking for a way to do that in PHP I also tried many regex by myself but nothing happened . Can someone provide me an example please ?
Reply With Quote
  #7 (permalink)  
Old 06-28-08, 12:47 PM
aditya2071990 aditya2071990 is offline
Newbie Coder
 
Join Date: Jun 2008
Posts: 25
Thanks: 0
Thanked 0 Times in 0 Posts
Nothing happened? Can you post your code here? Maybe there's only a small glitch in it that needs a smaller correction...
Reply With Quote
  #8 (permalink)  
Old 06-29-08, 09:40 AM
Mythvn Mythvn is offline
Wannabe Coder
 
Join Date: Jan 2006
Posts: 111
Thanks: 2
Thanked 0 Times in 0 Posts
PHP Code:

<?
$html 
file_get_contents('0.html');
// <font face="Tahoma"><font face="Verdana"><a href="http://google.com">Google</a></font>

$html str_replace('<font''<span'$html);
$html str_replace('</font''</span'$html);
$html preg_replace('/face\=\"(.*)\"/i''style="font-family:$1"'$html);
$html preg_replace('/size\=\"([0-9]{1,2})\"/i''style="font-size:$1pt"'$html);

echo 
$html;
// the output is 
// <span style="font-family:Tahoma"><span face="Verdana"><a href="http://google.com">Google</a></span>
// ???
?>
I'm a newbie with regex so I did this stupid things The "size" works fine but the "face" doesn't . And even the 2 work then after the replacement there would be 2 "style" attributes so I'm looking for a way to get rid both of them but I have no luck

Last edited by Mythvn; 06-29-08 at 09:49 AM.
Reply With Quote
  #9 (permalink)  
Old 06-30-08, 12:21 AM
sandeep.kumar's Avatar
sandeep.kumar sandeep.kumar is offline
Wannabe Coder
 
Join Date: Jun 2008
Location: India
Posts: 112
Thanks: 0
Thanked 0 Times in 0 Posts
Please try this code

PHP Code:



$html 
str_replace('<font''<span'$html);
$html str_replace('</font''</span'$html);
$html preg_replace('/face\=\"(.)\"/i''style="font-family:$1"'$html);
$html preg_replace('/size\=\"([0-9]{1,2})\"/i''style="font-size:$1pt"'$html); 
Reply With Quote
  #10 (permalink)  
Old 06-30-08, 03:05 AM
<?Wille?> <?Wille?> is offline
Junior Code Guru
 
Join Date: Jan 2004
Location: Helsinki, Finland
Posts: 666
Thanks: 0
Thanked 0 Times in 0 Posts
kinda complicated but i think i tackled it pretty well. conversion table due to the inconsistency with font size without a unit and style font-size. Size conversions appear to be true in FF3 and IE7

+ and - size default to medium in css if they exceed +-3
also didnt take in account percent values

didnt check if all font face's are fine as is in style font-family

umm, what else? well feel free to ask if theres anything unclear about the code or why its done
PHP Code:

<?
function size2pt($s) {
  if (!
ctype_digit($s) && !preg_match('~^[\+\-]~'$s)) { 
    return 
$s;
  } elseif (
preg_match('~^([\+\-]\d+)~'$s$m)) {
    
$size = array('-3' => 'xx-small''-2' => 'x-small''-1' => 'small''+1' => 'large''+2' => 'x-large''+3' => 'xx-large');
    if (
array_key_exists($m[0], $size))
      return 
$size[$m[0]];
    else
      return 
'medium';
  } else {
    
$size2pt = array(7.57.5101213.5182436);
    return (
array_key_exists($s$size2pt)?$size2pt[$s]:array_pop($size2pt)).'pt';
  }
}
function 
font2span($html) {
  
$match=array(
    
'~(?:face\=[\'"]?(\w+)[\'"]?)~i'// (?<=span(?:^\>)*) 
    
'~(?:size\=[\'"]?([\+\-]?\d+\w*)[\'"]?)~ie'
    
'~(?:color\=[\'"]?(#\d+|\w+)[\'"]?)~i',
    
'~<font(.*)>(.*)</font>~i');
  
$replace=array(
    
'font-family:$1;',
    
"'font-size:'.size2pt('$1').';'"
    
'color:$1;',
    
'<span style="$1">$2</span>');
  return 
preg_replace($match$replace$html);

}

$html '<html>
<head>
<title>title</title>
</head>
<body>
<div class=""><font face="Arial" size="+2" color="red">wer</font></div>
<font face=\'Arial\' size=10>Text</font>
<font face=Arial size=\'10pt\' color=\'#345345\'>Text</font>
<font face="Arial" size=10>Text</font>
</body>
</html>'
;

echo 
font2span($html);
?>
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
adoption-birth parent - script to match attributes philesq Script Requests 2 09-21-07 04:08 PM
About preg_match_all klaniak PHP 4 05-28-06 04:59 PM
PHP Regular Expressions Jaffizzle PHP 3 11-05-05 07:02 PM
help with preg_match maddude PHP 25 09-24-05 08:55 PM
Help with part of a PHP code cynebald PHP 3 07-29-05 05:07 AM


All times are GMT -5. The time now is 07:48 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.