Apr
30
2009
0

PHP’s function Code Highlighting

Just recently i discovered something very usefull. It could be very helpfull in any website or forum regarding programming.

There is a function in PHP called Code Highlighting. This would result in a very easy way of displaying php code onto a website. Just let me give an example.

<?php
highlight_string('<?php echo "test"; ?>');
?>

This outputted this in the displayed HTML:

<span style=”color: rgb(0, 0, 187);”>&lt;?php&nbsp;</span><span style=”color: rgb(0, 119, 0);”>echo&nbsp;</span><span style=”color: rgb(221, 0, 0);”>”test”</span><span style=”color: rgb(0, 119, 0);”>;&nbsp;</span><span style=”color: rgb(0, 0, 187);”>?&gt;</span>
</span>

Which looks just like this:
<?php echo “test”?>

As you can see, this can be very helpful.

Of course you need to be careful with this function as soon as you use it in “public”. You need to make sure you escape every quotes in the string with addslashes. This will prevent anything from happening.
Of course i can’t guarantee 100% protection. That’s pretty tricky. Good luck!
PS: http://nl2.php.net/manual/en/function.highlight-file.php

Written by Rene Pot in: Sugestions, general | Tags: , , , , , ,
Mar
02
2009
0

New function url_exists()

We all know the function file_exists() to check if a file exists on the local server.
But, if you want to check if an url is correct, this isn’t possible with this function.

Therefor i searched the web, and found this on php.net:

        function url_exists($url) {
            $hdrs = @get_headers($url);
            return is_array($hdrs) ? preg_match('/^HTTP\\/\\d+\\.\\d+\\s+2\\d\\d\\s+.*$/',$hdrs[0]) : false;
        }

Didn’t think it would be that easy! Good luck with it ;)

Written by Rene Pot in: PHP Problems, Sugestions | Tags: , , , ,
Dec
05
2008
0

Custom new function mkfile()

Since i needed to write a script that would make a file if it doesn’t excists, i thought mkfile() would be a perfect name for it.

The only thing mkfile does is check if a file excists on the server, and if it doesn’t it would create it, and chmod it. 

Let me show you the code:

<?php 
    function mkfile($filename,$mode) { 
        if (!file_exists($filename)) { 
             $handle = fopen($filename,'w+'); 
             fclose($handle);
             chmod($filename,$mode); 
        } 
    } 
?>

This code simply creates a function that can easily be used by calling it this way:

<?php 
    mkfile('pages/myfile.php',0777); 
?>

Just simply insert the filename and the rights (mode) and you can create a file with your own function.
You can also add your own features to this function, but in my opinion it is already complete.

Note: Don’t forget to check the input in the mkfile() function. If you let users create files, you should at least know they wont enter scripts in it! But i made this function only for you’re own use!

Written by Rene Pot in: Sugestions | Tags: , , ,
Oct
06
2008
2

Function Exists

Today i gotten into a problem. I am working on a project that supports multiple modules. And every module has it own functions. But sometimes a function is made double, because both modules need this function.
But you never can be sure both modules are implemented in the website. So, you need to include them twice.

But because PHP gives an error when a function is double redeclared, they have brought up a solution. It’s called function_exists.
Let me show you:

<?php
    if (function_exists('functionA')) {
       echo('It does');
    }
?>

This piece of code checks if functionA exists. But, ofcourse you want to use it so you will never include a double function. So you could use it this way:

<?php
   if (!function_exists('functionA')) {
       function functionA() {
           echo('it does not');
       }
   }
?>

Note the ! before function_exists. This turns an “if” into a “if not”. You can put it in front of every if quote.

Written by Rene Pot in: PHP Problems | Tags: , , , ,

Powered by WordPress | Aeros Theme | TheBuckmaker.com WordPress Themes