Jump to content

Recommended Posts

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.

[code]
<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);

}

?>[/code]

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):

[code]
<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);
    
}
?>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/11068-gettype-giving-me-wrong-type/
Share on other sites

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--]
[!--quoteo(post=379538:date=Jun 3 2006, 12:33 AM:name=jeremywesselman)--][div class=\'quotetop\']QUOTE(jeremywesselman @ Jun 3 2006, 12:33 AM) [snapback]379538[/snapback][/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?
I think it returs a string every time because it is in quotes. I did a few tests and this:

[code]<?php
$text = 23;
?>[/code]

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

[!--coloro:#990000--][span style=\"color:#990000\"][!--/coloro--]Jeremy[!--colorc--][/span][!--/colorc--]
[!--quoteo(post=379563:date=Jun 3 2006, 03:09 AM:name=jeremywesselman)--][div class=\'quotetop\']QUOTE(jeremywesselman @ Jun 3 2006, 03:09 AM) [snapback]379563[/snapback][/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:

[code]<?php
$text = 23;
?>[/code]

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.
[!--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
[!--quoteo(post=379598:date=Jun 3 2006, 08:10 AM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Jun 3 2006, 08:10 AM) [snapback]379598[/snapback][/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
[!--quoteo(post=379684:date=Jun 3 2006, 02:40 PM:name=poirot)--][div class=\'quotetop\']QUOTE(poirot @ Jun 3 2006, 02:40 PM) [snapback]379684[/snapback][/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?
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.