taith Posted December 22, 2006 Share Posted December 22, 2006 does anybody have a funtion that would change[code]<font color=""></font>[/code]into[code]<span style="color:;"></font>[/code]or any clues as to where should start to build one ? Quote Link to comment https://forums.phpfreaks.com/topic/31580-solved-font-gtspan/ Share on other sites More sharing options...
Chronos Posted December 22, 2006 Share Posted December 22, 2006 That's a regex question, please post it in the appropiate subforum :) they can help you Quote Link to comment https://forums.phpfreaks.com/topic/31580-solved-font-gtspan/#findComment-146368 Share on other sites More sharing options...
HuggieBear Posted December 22, 2006 Share Posted December 22, 2006 Is it only the colour you want to change or all the elements in the font tag? Are you trying to make it more standards compliant?If so, the first place I'd start would be the [url=http://www.w3c.org]w3c[/url] where you can find out about the spec of the html <font> tag. See what arguments are allowed and what you should be expecting to come across when changing the code.I think ultimately you'll be looking at a regular expression for this sort of task.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/31580-solved-font-gtspan/#findComment-146371 Share on other sites More sharing options...
taith Posted December 22, 2006 Author Share Posted December 22, 2006 i want to change all "font" classes to "span style=""" Quote Link to comment https://forums.phpfreaks.com/topic/31580-solved-font-gtspan/#findComment-146376 Share on other sites More sharing options...
HuggieBear Posted December 22, 2006 Share Posted December 22, 2006 I think with <font> you're only dealing with the [i]size[/i], [i]color[/i] and [i]face[/i] attributes anyway, so it shouldn't be too difficult.In addition to that you may see these in the tag too... [i]id, class, lang, dir, title, style[/i]So that's what you should be expecting when searching for your match.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/31580-solved-font-gtspan/#findComment-146379 Share on other sites More sharing options...
taith Posted December 22, 2006 Author Share Posted December 22, 2006 i know i need these to transferid, class, color, size, faceand... just because i know i'll need it... i also need to make one going back. Quote Link to comment https://forums.phpfreaks.com/topic/31580-solved-font-gtspan/#findComment-146381 Share on other sites More sharing options...
HuggieBear Posted December 22, 2006 Share Posted December 22, 2006 Here's where I'd start.[code]<?php// Initial data$data = <<<HTML <font face="arial" size="2" color="#FFFFFF">This is my text</font>HTML;// Match the font tagspreg_match("/<font (.*?)>/", $data, $matches);// Place elements into an array keyed by attribute$elements = explode(" ", $matches[1]);foreach ($elements as $att){ list($k,$v) = explode("=", $att); $attributes[$k] = $v;}// Check we've captured the right dataecho "<pre>\n";print_r($attributes);echo "</pre>\n";?>[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/31580-solved-font-gtspan/#findComment-146388 Share on other sites More sharing options...
taith Posted December 22, 2006 Author Share Posted December 22, 2006 heres what i've gotten so far... but for some reason the "color:" is exiting after every loop... any ideas?[code]<?function fonttospan($string){ $tam=strlen($string); $newstring=""; $tag=0; for($i=0; $i<$tam; $i++){ if(($string{$i}.$string{$i+1}.$string{$i+2}.$string{$i+3}.$string{$i+4} == '<font')||($tag>0)){ $pre = substr($newstring, 0, $i); if($tag==0) $span='<span style="'; if($string{$i}.$string{$i+1}.$string{$i+2}.$string{$i+3}.$string{$i+4} == 'color'){ $span.='color:'; } if($tag==0) $span.='">'; $tag=1;echo $span; }elseif($string{$i-1} == '>'){ $newstring=$pre.$span;echo 'ASDF'; $tag=0; } if($tag==0) $newstring .= $string{$i}; } $newstring= str_replace("</font>", "</span>", $newstring); return $newstring;}echo fonttospan('a<font color="" size="" id="" class="">text</font>');?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/31580-solved-font-gtspan/#findComment-146390 Share on other sites More sharing options...
taith Posted December 22, 2006 Author Share Posted December 22, 2006 ok... heres what i got sofar, its replacing all the values, but its not continuing after the "</font>"[code]<?function fonttospan($string){ $tam=strlen($string); $newstring=""; $tag=0; for($i=0; $i<$tam; $i++){ if($string{$i} == '>'){ if((!empty($style))||(!empty($class))||(!empty($id))){ if(!empty($style)) $style='style="'.$style.'"'; $span='<span '.$style.' '.$class.' '.$id; } if(!empty($pre)) $newstring=$pre.$span; unset($pre,$span); $tag=0; }elseif(($string{$i}.$string{$i+1}.$string{$i+2}.$string{$i+3}.$string{$i+4} == '<font')||($tag>0)){ if($tag==0) $pre = substr($newstring, 0, $i); if($string{$i}.$string{$i+1}.$string{$i+2}.$string{$i+3}.$string{$i+4} == 'color'){ $style.='color:'; for($i=$i; $found!=2; $i++){ if($string{$i}=='"') $found++; elseif($found>0) $style.=$string{$i}; } $style.=';'; }elseif($string{$i}.$string{$i+1}.$string{$i+2}.$string{$i+3} == 'size'){ $style.='font-size:'; for($i=$i; $found!=2; $i++){ if($string{$i}=='"') $found++; elseif($found>0) $style.=$string{$i}; } $style.=';'; }elseif($string{$i}.$string{$i+1} == 'id'){ $id='" id="'; for($i=$i; $found!=2; $i++){ if($string{$i}=='"') $found++; elseif($found>0) $id.=$string{$i}; } $id.='"'; }elseif($string{$i}.$string{$i+1}.$string{$i+2}.$string{$i+3}.$string{$i+4} == 'class'){ $class='" class="'; for($i=$i; $found!=2; $i++){ if($string{$i}=='"') $found++; elseif($found>0) $class.=$string{$i}; } $class.='"'; } unset($found); $tag=1; } if($tag==0) $newstring.= $string{$i}; } return str_replace("</font>", "</span>", $newstring);}echo fonttospan('a<font color="#asdf34" size="3px" id="asdf">text</font>');?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/31580-solved-font-gtspan/#findComment-146419 Share on other sites More sharing options...
obsidian Posted December 22, 2006 Share Posted December 22, 2006 Here's something I worked up that seems to do alright. I didn't examine all the other proposed solutions, so forgive me if I'm duplicating something:[code]<?phpfunction fontToSpan($String) { preg_match_all('|<(font)([^>]*)>(.*?)</\1>|i', $String, $matches); foreach ($matches[2] as $match) { $newTag = "<span"; $styles = array(); preg_match_all('/(id|class|color|size|face)\=(["\'])(.*?)\2/i', $match, $x); foreach ($x[1] as $key => $attr) { switch ($attr) { case "id": $newTag .= " id=\"{$x[3][$key]}\""; break; case "class": $newTag .= " class=\"{$x[3][$key]}\""; break; case "color": $styles[] = "color: {$x[3][$key]}"; break; case "size": $styles[] = "font-size: {$x[3][$key]}"; break; case "face": $styles[] = "font-family: {$x[3][$key]}"; break; } } } $newStyle = implode('; ', $styles); $newTag .= " style=\"$newStyle\">{$matches[3][0]}</span>"; $String = str_replace($matches[0], $newTag, $String); return $String;}$contents = "<font id=\"myId\" class='myclass' color=\"blue\" size=\"4\" face=\"Arial\">Some Text goes here!!!</font>";$contents = fontToSpan($contents);echo $contents;?>[/code]You'll notice that this function keeps the id and class intact while creating an inline style attribute to contain all the other font attributes you have mentioned. The biggest flaw in this, however, is the size attribute. Because with a font tag, you simply declare a number, this will cause issues when creating the font-size attribute in CSS. You'll need to come up with some sort of algorithm to account for that. Otherwise, I think you'll find that it really generates pretty good code.Hope this helps. Quote Link to comment https://forums.phpfreaks.com/topic/31580-solved-font-gtspan/#findComment-146428 Share on other sites More sharing options...
taith Posted December 22, 2006 Author Share Posted December 22, 2006 sweet deal :-P thanks :-P Quote Link to comment https://forums.phpfreaks.com/topic/31580-solved-font-gtspan/#findComment-146429 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.