Jump to content

Recommended Posts

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.

Regards
Huggie
Link to comment
https://forums.phpfreaks.com/topic/31580-solved-font-gtspan/#findComment-146371
Share on other sites

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.

Regards
Huggie
Link to comment
https://forums.phpfreaks.com/topic/31580-solved-font-gtspan/#findComment-146379
Share on other sites

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 tags
preg_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 data
echo "<pre>\n";
print_r($attributes);
echo "</pre>\n";
?>[/code]

Regards
Huggie
Link to comment
https://forums.phpfreaks.com/topic/31580-solved-font-gtspan/#findComment-146388
Share on other sites

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]
Link to comment
https://forums.phpfreaks.com/topic/31580-solved-font-gtspan/#findComment-146390
Share on other sites

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]
Link to comment
https://forums.phpfreaks.com/topic/31580-solved-font-gtspan/#findComment-146419
Share on other sites

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]
<?php
function 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.
Link to comment
https://forums.phpfreaks.com/topic/31580-solved-font-gtspan/#findComment-146428
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.