Jump to content

[SOLVED] str_replace


the-botman

Recommended Posts

ok hi guys ...

 

i am not sure if i am posting my question in the rite place but here goes.

i want to str_replace all numbers between 0-60 and change the colour of the numbers this is what i have and it works but using this way i need to have 60 lines of code is a any other way?

 

$Fguide = str_replace('0','<font face="Verdana" color="red" size="2">0</font>',$Fguide);

Link to comment
https://forums.phpfreaks.com/topic/181783-solved-str_replace/
Share on other sites

i am sorry i am very new to php , ok see i am pulling the tv guide from a site to mine it displays like this http://bhawap.com/TvGuide-sabc1.php and your code works but only if u change it to

$i=2; 
$upper = 60;

while ($i <= $upper) {
    $FGuide = str_replace($i, '<font face="Verdana" color="red" size="2">' . $i . '</font>', $FGuide);
    $i++;
}

will look like http://bhawap.com/TvGuide-etv.php but if i add it the way u show me will display like http://bhawap.com/TvGuide-sabc2.php

Link to comment
https://forums.phpfreaks.com/topic/181783-solved-str_replace/#findComment-958727
Share on other sites

You want the reason, because after you iterate it up passed 0 and 1 it replaces 2 which there are "2"'s that are inside the tags, the flaw is that there are tags in the code and of course if a tag is created within a tag it will cause issues:

 

<font face="Verdana" color="red" size="<font face="Verdana" color="red" size="2">2</font>">0</font>	

 

Notice how it is replacing the 2 inside the <font> tag, which of course will cause display issues.

 

The code I provided works, it is just that since the code replaced has a 2 in it, causes a little issue that anything replaced before that will be replaced.

 

A better method of doing this, use a CSS style so you do not get this error.

 

<style type="text/css">
.redColor {
    color: red;
    font-size: 15px;
    font-family: Verdana;
}
</style>

<?php
$i=0; 
$upper = 60;

while ($i <= $upper) {
    $FGuide = str_replace($i, '<font style="redColor">' . $i . '</font>', $FGuide);
    $i++;
}
?>

 

Will solve that problem.

 

EDIT: I should have caught that ahead of time, sorry for that.

Link to comment
https://forums.phpfreaks.com/topic/181783-solved-str_replace/#findComment-958734
Share on other sites

ok thanks alot for all your help and sorry again i also found a way to make it work hehe but yours is the best as always hehe here was my way

$i=2; 
$upper = 60;

while ($i <= $upper) {
    $FGuide = str_replace($i, '<font face="Verdana" color="red" size="2">' . $i . '</font>', $FGuide);
    $i++;
}
    $FGuide = str_replace('0', '<font face="Verdana" color="red" size="2">0</font>', $FGuide);
  $FGuide = str_replace(1, '<font face="Verdana" color="red" size="2">1</font>', $FGuide);


return $FGuide;

thanks again i will close this topic if u dont reply in 20 min hehe cheers

Link to comment
https://forums.phpfreaks.com/topic/181783-solved-str_replace/#findComment-958740
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.