Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Sunday, March 18, 2012

Problem with strtotime in PHP

Today I found an interesting issue. Please try this following PHP code:
print strtotime("18/03/2012 06:08 am");
I didn't see any output. Then I looked at the documentation and found the following line:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

So I had to replace "/" with "-" using str_replace and that worked!
print strtotime("18-03-2012 06:08 am");
Output:
1332029280

Monday, March 5, 2012

Phone Number Extractor in PHP

Here is a function to parse the content of a web page and extract phone / fax numbers (USA only). Regular expression is used to parse phone numbers.

 function extract_phone_numbers ($html_content)    
 {    
     $html_content = preg_replace("/[\s\+\-\(\)\.]/", "", $html_content);    
     $ara = array();    
     if (preg_match_all('/\D(\d{10})\D/', $html_content, $data)) {        
         $ara = array_unique($data[1]);        
     }    
     if (preg_match_all('/1(\d{10})\D/', $html_content, $data)) {        
         $ara = array_unique(array_merge($ara, $data[1]));    
     }    
     if (preg_match_all('/^(\d{10})$/', $html_content, $data)) {        
         $ara = array_unique(array_merge($ara, $data[1]));    
     }    
     return $ara;    
 }    

Email extractor in PHP using regex

Today, I am sharing my PHP code that extracts email address from html source of an URL. It uses regular expression to parse email address.

 function extract_email_addresses ($html_source)    
 {    
     $html_source = str_replace("(at)", "@", $html_source);    
     $html_source = str_replace("[at]", "@", $html_source);    
     $html_source = str_replace("(dot)", ".", $html_source);    
     $html_source = str_replace("[dot]", ".", $html_source);    
     $html_source = strtolower($html_source);    
     $ara = array();    
     if (preg_match_all('/([_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3}))/', $html_source, $data)) {        
         $ara = array_unique($data[1]);    
     }    
     return $ara;    
 }    
Please share your thoughts to improve the function.

Wednesday, May 13, 2009

Trouble with Facebook Client API

I have working on facebook application development (it's a game named Fighter Jets), and facing some weired problem. I have decided to share my experience here.

Today I tried the post link feature of the facebook API. First I tried the following code:
$facebook->api_client->links_post($user_id, 'http://khaaan.com/','Best. Website. Ever.');
I got it from their API documentation.

I got error using it. It says that 'The url you supplied is invalid'. May be it's invalid. Then I changed the URL to http://www.google.com, which is valid definitely! Still getting the same problem!! WTF!

Then I searched their forum and found that I am not the only victim, some people already faced this problem and one of them solved the problem in the following way:
$facebook->api_client->links_post('http://khaaan.com/','Best. Website. Ever.', $user_id);

Which works! Needed to use the user id as the third parameter, but from the example in the API documentation we see that it's the first parameter.

I really don't understand why the documentation is wrong.

Tuesday, August 12, 2008

Send email using PHP

Sending emails using a PHP script / program is very easy. Just use the mail function: mail($to, $subject, $body);

Check mail function for details.

There can be some problems if it's not configured properly. Check the installation / configuration page for details.

Friday, August 8, 2008

Count number of elements in array - PHP, Perl, Python

We often need to count the number of elements in an array (or get the size of an array). Here is how to do it in PHP, Perl & Python:

PHP: $items = count($ara);

Perl: my $items = scalar(@ara);, you can also try: my $items = @ara;

Python: items = ara.__len__(). Note that in Python it's called list. List can be used as array.

Wednesday, June 4, 2008

execute linux command using Perl, Python, PHP

Those who program in Linux often need to execute/run linux commands from programs/scripts.

Using python you can execute linux command is the following way:

import os
os.system('ls -lt')



If you use Perl, you can do it in the following ways:
1. system(mkdir $folder);
2. qw(mkdir $folder);
3. $temp = `mkdir $folder`; #back ticks
4. exec($command);

If you use back ticks or qw, $? will be set if any error occurs. So you can use it like this:

qw(rm $filename);
if($?) {
print STDERR "error deleting file\n";
}


There are also several options available to run linux commands using PHP. Take a look at here for detals: http://us2.php.net/manual/en/ref.exec.php

Saturday, May 3, 2008

SimpleXML - simple way to parse XML in PHP

This morning I needed to write a PHP script that parses an XML file. I could do it easily with regular expressions - as I can play very well with regexp :). But I was looking for an easier & simpler way to parse XML using PHP, as I thought there must be a very simple way. And I was right, I found SimpleXML. SimpleXML makes XML processing really simple. Here I give links of some excellent (and simple!) resources of SimpleXML:
SimpleXML will definitely speed up your XML processing speed!