Jump to content


Photo

gettype() giving me wrong type


  • Please log in to reply
8 replies to this topic

#1 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 03 June 2006 - 04:08 AM

What i plan on doing is, when i post code on my website, i want it to be colored and pretty (like dreamweaver as you type).

So i am assuming the best way of doing this, is use gettype() for every word and assign different colors to different types.


the only problem is, gettype() is saying everyting is a string, even when i enter a number in the textarea.

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<textarea cols="40" rows="9" name="text"></textarea><br />
<input type="submit" value="submit" name="submit">
</form>

<?php

if(isset($_POST['submit'])) {

    $text = $_POST['text'];

    echo gettype($text);

}

?>

you can see it in action here: [a href=\"http://ficti0n.com/del.php\" target=\"_blank\"]http://ficti0n.com/del.php[/a]

So i thought maybe i should use a bunch of if statements. something the following:

if($text == 1 || 2 || 3 || 4 || 5 || 6 || 7 || 8 || 9 || 0) {
$font = '<font color="red">';
}

But then i thought, how would do that with classes and objects?



Any help is appreciated. Thanks/



(btw this is just a sample of the code extracted from the real one. in case it makes a difference, here's everything i have):

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<textarea cols="40" rows="9" name="text"></textarea><br />
<input type="submit" value="submit" name="submit">


</form>
<?php

function evaluate($text) {

    $exploded = explode(' ', $text);
    $number_of_words = sizeof($exploded);
    
    
    for ($i = 0; $i < $number_of_words; $i++) {
        echo $i.') '.$exploded[$i].' <---'.gettype($exploded[$i]).'<br> ';
    }
}

if(isset($_POST['submit'])) {

$text = $_POST['text'];

evaluate($text);
    
}
?>

regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#2 jeremywesselman

jeremywesselman
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts
  • LocationIndependence, KY

Posted 03 June 2006 - 04:33 AM

If you want to check for a number, you can use [a href=\"http://www.php.net/manual/en/function.is-numeric.php\" target=\"_blank\"]is_numeric()[/a].

There are also string checks.

[!--coloro:#990000--][span style=\"color:#990000\"][!--/coloro--]Jeremy[!--colorc--][/span][!--/colorc--]

#3 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 03 June 2006 - 06:48 AM

[!--quoteo(post=379538:date=Jun 3 2006, 12:33 AM:name=jeremywesselman)--][div class=\'quotetop\']QUOTE(jeremywesselman @ Jun 3 2006, 12:33 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
If you want to check for a number, you can use [a href=\"http://www.php.net/manual/en/function.is-numeric.php\" target=\"_blank\"]is_numeric()[/a].

There are also string checks.

[!--coloro:#990000--][span style=\"color:#990000\"][!--/coloro--]Jeremy[!--colorc--][/span][!--/colorc--]
[/quote]


Thank you. php.net has this:

is_bool()
is_null()
is_float()
is_int()
is_string()
is_object()
is_array()


But, why is gettype() returning a string every time?

regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#4 jeremywesselman

jeremywesselman
  • Members
  • PipPipPip
  • Advanced Member
  • 154 posts
  • LocationIndependence, KY

Posted 03 June 2006 - 07:09 AM

I think it returs a string every time because it is in quotes. I did a few tests and this:

<?php
$text = 23;
?>

returns integer. But if I put it in quotes, it returns a string.

[!--coloro:#990000--][span style=\"color:#990000\"][!--/coloro--]Jeremy[!--colorc--][/span][!--/colorc--]

#5 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 03 June 2006 - 07:47 AM

[!--quoteo(post=379563:date=Jun 3 2006, 03:09 AM:name=jeremywesselman)--][div class=\'quotetop\']QUOTE(jeremywesselman @ Jun 3 2006, 03:09 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
I think it returs a string every time because it is in quotes. I did a few tests and this:

<?php
$text = 23;
?>

returns integer. But if I put it in quotes, it returns a string.

[!--coloro:#990000--][span style=\"color:#990000\"][!--/coloro--]Jeremy[!--colorc--][/span][!--/colorc--]
[/quote]


right. that works fine.

But in the example i have posted, i didn't put it in quotes. It was taken from the text area.
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 03 June 2006 - 12:10 PM

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]But in the example i have posted, i didn't put it in quotes. It was taken from the text area.[/quote]
That is because everything returned from a form is type "text" by definition.

If you want to show PHP code in color, you should look at the [a href=\"http://www.php.net/highlight_string\" target=\"_blank\"]highlight_string()[/a] and [a href=\"http://www.php.net/highlight_file\" target=\"_blank\"]highlight_file()[/a] functions.

Ken

#7 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 03 June 2006 - 06:02 PM

[!--quoteo(post=379598:date=Jun 3 2006, 08:10 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Jun 3 2006, 08:10 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
That is because everything returned from a form is type "text" by definition.

If you want to show PHP code in color, you should look at the [a href=\"http://www.php.net/highlight_string\" target=\"_blank\"]highlight_string()[/a] and [a href=\"http://www.php.net/highlight_file\" target=\"_blank\"]highlight_file()[/a] functions.

Ken
[/quote]

Thank you very much.
No idea that function existed\

However, i'd like to specify my own colors. so ill have to make my own version of it
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#8 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 03 June 2006 - 06:40 PM

You can change the color on runtime, using ini_set():

[a href=\"http://www.php.net/manual/en/ref.misc.php#ini.syntax-highlighting\" target=\"_blank\"]http://www.php.net/manual/en/ref.misc.php#...ax-highlighting[/a]
~ D Kuang

#9 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 03 June 2006 - 11:34 PM

[!--quoteo(post=379684:date=Jun 3 2006, 02:40 PM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ Jun 3 2006, 02:40 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
You can change the color on runtime, using ini_set():

[a href=\"http://www.php.net/manual/en/ref.misc.php#ini.syntax-highlighting\" target=\"_blank\"]http://www.php.net/manual/en/ref.misc.php#...ax-highlighting[/a]
[/quote]


Thank you.
Quick question:
I see i can use highlight_file, highlight_string and show_source to display souce code. But they all highlight the syntax.

Im on a mission here to make a syntax-highlighting function on my own, so i'd like the text not to comeback highlited by defautl. can that happen?
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users