May
06
2009
0

Fresh Start

Because of my increase in experience in more languages than just PHP, i decided to make a suitable blog for that matter.

Therefor I decided to move any future posts to a new domain. And this will be www.renepot.com

I hope to see you all there!

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: , , , , , ,
Apr
27
2009
0

PHP 5 Recipes: A Problem-Solution Approach

Recently I bought this PHP book, and i must say, it’s a good one! There is more than just some examples. They show you how to fix a problem the best way! It is really worth buying.

Just want to recommend a good book!

PHP 5 Recipes: A Problem-Solution Approach


Apr
24
2009
0

Mysql Full Text Search - Speed up those results

I just ran into the problem of a very big database, and a very slow search function..

At first i was a little confused, but soon i came to the conclusion of building a Full Text Search function.

The first problem on building this i encountered was the database coalition. You have to have a MyISAM database. So, that’s what i did.

  • Create a table:

CREATE TABLE news (content TEXT, title VARCHAR(250), id INT(11) NOT NULL auto_increment, PRIMARY KEY(id);

  • Add a fulltext index:

ALTER TABLE articles ADD FULLTEXT(content, title);

This FULLTEXT index makes an index on both content and title, so you can’t search just one of them.

  • Create a Query:
$query = "SELECT title,content,id, MATCH(title,content) AGAINST ('my search') AS score FROM news WHERE MATCH(title,content) AGAINST ('my search')";

Let me explain:

SELECT MATCH(title,content) AGAINST (’my search’) means you select the result of a MATCH. The result is a score. The higher the number, the better the result.

AGAINST (’my search’) searches in the selected fields in the MATCH (title,content) in my example. The search can be multiple words. The search searches for all words individually. You can also add a minus (-) and a plus (+) to a word, in order to change the priority. A minus tries searching a row without the word, a plus makes a word more imporant.

The reason the MATCH AGAINST is double in my query is just because i want to order my results (2nd time) and i want the score visible (1st time). I rename my MATCH AGAINST as Score, in order to get the score back out of the query. This would be something you could show on your page. Or do something with in your code.

If you want to limit the result by score, you have to add the following to the query:

HAVING score > 2 (you can change the number in anything you want, but this will filter the worst results)

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
28
2008
2

Read and Download a XML File

I was working on a project when i needed to download an XML file and store it localy, to prevent slow scripts, because this XML file had 47 items.

When i would store it my script would work much faster! So i searched, and asked and found this:

<?php
$geturl = 'http://www.site.com/example.xml';
$savelocal = 'file.xml';
file_put_contents( $savelocal, file_get_contents( $geturl ) );
?>

It is this simple and it works totaly fine!

Written by Rene Pot in: PHP Problems | Tags: , , , , , ,
Oct
23
2008
0

Get timestamps out of week no. and year.

Today i had a problem i only had a week number and a year. And i needed timestamps to get all results from a database in that week.

So, i searched on google and found this as first result:
http://tzzz.wordpress.com/2006/08/14/8/

The code he provided wasn’t exactly what i have been looking for but in the comments there was a result that was very close.

function getFirstDayOfWeek($year, $weeknr){
 
   $offset = date('w', mktime(0,0,0,1,1,$year));
   $offset = ($offset < 5) ? 1-$offset : 8-$offset;
   $monday = mktime(0,0,0,1,1+$offset,$year);
   return strtotime('+' . ($weeknr - 1) . ' weeks', $monday);
}

Now i changed this code to provide me the first and last day of the week:

function getFirstDayOfWeek($year, $weeknr){
 
    $offset = date('w', mktime(0,0,0,1,1,$year));
    $offset = ($offset < 5) ? 1-$offset : 8-$offset;
    $monday = mktime(0,0,0,1,1+$offset,$year);
    $sunday = mktime(0,0,0,1,7+$offset,$year);
 
    $day['first']=strtotime('+' . ($weeknr - 1) .  'weeks', $monday);
    $day['last']=strtotime('+' . ($weeknr - 1) . ' weeks', $sunday);
 
return($day);
}

This solved my entire problem, so i could search my database with weeks without having it to store it in the database. That would be a solution that would cost me to much data.

Written by Rene Pot in: PHP Problems | Tags: , , , , , , ,
Oct
14
2008
3

MySQL issue with character set

Today i stumbled upon an issue with inserting text into a database, and the character set wasn’t taking it. No mather what i changed in the database, it was inserted wrong time after time.

Characters like â and é are shown wrong. In the variable it is correct, but in database it isn’t.

This is what i had:

<?php
   $string = "é á â ê ô ç ö";
   $db->query(" UPDATE mytable SET mytxt = '".$string."' WHERE id=1"); // using ezSQL
 
?>

But when i looked into the database it wasn’t correct. Wrong encoding. The tabel was set UTF-8 so that couldn’t be wrong.

This was my solution:

<?php
   $string = "é á â ê ô ç ö";
   $db->query(" SET CHARACTER SET utf8 ");
   $db->query(" UPDATE mytable SET mytxt = '".$string."' WHERE id=1"); // using ezSQL
 
?>

Now it was correct inserted, so i could use it properly.

Written by Rene Pot in: PHP Problems, 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