Read CSV file with PHP

Reading the content of a CSV file can be done with PHP easily; thanks to PHP built-in function fgetcsv. Below is the sample code to read CSV file (comma separated). The content of the CSV file is shown below.

Sales per State,CA,AZ,TX,NY
Jan,323,324,5435,5334
Feb,423,6646,54,34
Mar,422,645,545,23
Apr,565,477,443,232
May,634,6454,54354,2323
Jun,645,656,433,4242
July,333,7565,3433,53535

Below is the PHP code to read a CSV file.

<?php
 
$file = 'TestData1.csv'; 
 
//The delimiter that is used in csv file.
$delimiter = ',';
 
//Open the TestData1.csv file for reading.
$handle = fopen($file, "r");
 
if($handle)
{
	//Read each line and print the line out.
	while (($line_array = fgetcsv($handle, 4000, $delimiter)) !== false) {
 
		//Do something to each line.
		echo "<pre>";
		print_r($line_array);
		echo "</pre>";
	}
	fclose($handle);
}
 
?>

Once the CSV file is opened using fopen, we can read each line in the input file. The function fgetcsv takes one required parameter and 2 optional parameters. The first parameter is the file pointer to a file successfully opened by fopen function. The second parameter is the number of characters that fgetcsv should read from a line in the CSV file. In this example, I set this parameter to 4000 characters. If this second parameter is set to zero, then fgetcsv will read the entire line no matter how long the line is. However, setting this parameter to zero might take PHP more time to read a line if your CSV file is long and complex. The third parameter is the delimiter that is used in the CSV file. By default, the third parameter is set to a comma.

Posted in PHP | Leave a comment

Facebook with facial recognition feature, privacy invasion?

According to a blog post on Facebook, Facebook has been rolling out a facial recognition feature which, Facebook claims, will help users to tag their photos easier. This new tagging feature will attempt to recognize your face and your friend’s faces then suggesting people names to make photo tagging an easier task.

This image is from Facebook.

On one hand, this feature seems to be useful. On the other hand, this will definitely make lot of users raise their eyebrow due to privacy concern. Facebook users seem to have strong reaction to this new features. Check out list of comments from Facebook users.

Posted in News | Tagged | Leave a comment

sugarCRM view sugarBean of a module

It was hard for me to work with a sugarCRM module at first without seeing its sugarBean structure and what kind of data or information being populated in a sugarBean. So, I found a way to access any sugarBean object of a module as described below.

Let’s say I want to see the sugarBean of a Call which is a part of Activities module. I would do the following.

1. Put the following code in the index.php file (near the end of the file) and save.

require_once("modules/Calls/Call.php");
$c = new Call();
$c->retrieve("5065c3c0-c95c-a212-cf6f-4dcb0c584b01");
echo "<pre>";
print_r($c);
echo "</pre>";

2. Now, go to the web browser and view a webpage in sugarCRM system, you can see the  sugarBean object for Call as shown in the picture below.

SugarCRM call bean output

Code Explanation

The first three lines of the code above includes the path to the Call class, instantiate a new object for a Call and retrieve a record for a Call. If you have created a call in sugarCRM, you can find the id in table “calls”, then pass that id in function “retrieve(call_id)” above.

The rest of the code just displays the sugarBean object for a call referenced by variable $c

To view other sugarBean objects for other modules, you can follow similar steps described above.

Posted in PHP | Tagged , | Leave a comment

sugarCRM view PHP error/warning/notice

One quick way to view PHP error, warning or notice in sugarCRM system is to turn on error_reporting in php.

You can do this  within sugarCRM if you have limited access to php.ini file.

Add this code to the top of index.php file in sugarCRM

  1. <?php
  2. if(!defined('sugarEntry'))define('sugarEntry', true);
  3.  
  4. //debug setting.
  5. ini_set('display_errors',1);
  6. error_reporting(E_ALL);
  7.  
  8. //......the rest of php code in index file

Now, if you have any error in your php code, you’ll see it on sugarCRM webpages.

Make sure you remove the debug setting code once you are done with debugging sugarCRM.

Posted in PHP | Tagged , | Leave a comment

Check if a webpage or url is indexed by google

To check if a web page or url is indexed by google, you can do the following:

1. Go to Google.com

2. In the search box, type in “Info:” and the webpage or url you want to check. So, If i want to check if google indexed this url “http://bitonwire.com/jblogs/get_facebook_like_button_statistics“, I would type in the search box
info:http://bitonwire.com/jblogs/get_facebook_like_button_statistics

then hit Search.

If google indexed the url above already, I would see one search result being returned. Otherwise, I would see a message similar the message below:

Sorry, no information is available for the URL bitonwire.com/fasdfas

  • If the URL is valid, try visiting that web page by clicking on the following link: bitonwire.com/fasdfas
  • Find web pages from the site bitonwire.com/fasdfas
  • Find web pages that contain the term “bitonwire.com/fasdfas”
Posted in C# Programming | Tagged | 1 Comment

Get facebook Like button statistics with PHP

Facebook API allows us to retrieve the statistics for “Like” button given a “liked” URL.  The function get_facebook_like_button statistics returns xml result from facebook for a “liked” URL. This function utilizes CURL in PHP to retrieve the data from facebook. You should have CURL enable in order to use this function. To check if CURL is enable in apache, use phpinfo() function.

/*
*   Return statistics of a facebook like button for a given URL.
*   Data return is in XML format.
*
*/
function get_facebook_like_button_statistics($url)
{
	$fb_url_query = "http://api.facebook.com/method/fql.query?query=";
	$query = "SELECT  like_count, total_count, share_count, click_count from link_stat  where  url='".$url."'";
	$fb_url_query = $fb_url_query.urlencode($query);
 
	$c = curl_init();
	$timeout = 10;
	curl_setopt($c,CURLOPT_URL,$fb_url_query);
	curl_setopt($c,CURLOPT_RETURNTRANSFER,1);
	curl_setopt($c,CURLOPT_CONNECTTIMEOUT,$timeout);
	$data = curl_exec($c);
	curl_close($c);
	return $data;
}

Sample Code

$url = "http://sports.yahoo.com/mlb/blog/big_league_stew/post/TMI-Fan-8217-s-plan-to-propose-to-Ryan-Braun-b?urn=mlb-wp4407";
 
$data = get_facebook_like_button_statistics($url);
 
echo($data);

Output

<?xml version="1.0" encoding="UTF-8"?>
<fql_query_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" list="true">
  <link_stat>
    <like_count>266</like_count>
    <total_count>1629</total_count>
    <share_count>944</share_count>
    <click_count>0</click_count>
  </link_stat>
 
</fql_query_response>

You can use PHP xml parser to parse the data returned by facebook or use php DOM.

Posted in PHP | Tagged | 2 Comments

Enable Curl in XAMPP

To enable Curl in XAMPP, you need to do the following:

1. uncomment the following line in php.ini files:
;extension=php_curl.dll

2. php.ini files can be found in the following locations (depends on which directory you installed XAMPP on your computer. I installed xampp right under C drive)

C:\xampp\apache\bin\php.ini
C:\xampp\php\php.ini
C:\xampp\php\php4\php.ini

OR

C:\Program Files\xampp\apache\bin\php.ini
C:\Program Files\xampp\php\php.ini
C:\Program Files\xampp\php\php4\php.ini

3. Restart your apache server

4. Check to see if curl is enable using phpinfo() function

Posted in PHP | Tagged , | Leave a comment

youtube embed code with PHP

The get_youtube_embed function below generates embed code for a youtube video given its id. You also have the option to set whether the video should be played automatically or not once the web page is loaded.

/*
*	get_youtube_embed returns embed code for a youtube video given its id.
*/
function get_youtube_embed($youtube_video_id, $autoplay=false)
{
	$embed_code = "";
 
	if($autoplay)
		$embed_code = '<embed src="http://www.youtube.com/v/'.$youtube_video_id.'&rel=1&autoplay=1" pluginspage="http://adobe.com/go/getflashplayer" type="application/x-shockwave-flash" quality="high" width="480" height="395" bgcolor="#ffffff" loop="false"></embed>';
	else
		$embed_code = '<embed src="http://www.youtube.com/v/'.$youtube_video_id.'&rel=1" pluginspage="http://adobe.com/go/getflashplayer" type="application/x-shockwave-flash" quality="high" width="450" height="376" bgcolor="#ffffff" loop="false"></embed>';
	return $embed_code;
}

Sample Code:

Get the youtube embed code for this video http://www.youtube.com/watch?v=5P6UU6m3cqk

echo get_youtube_embed("5P6UU6m3cqk");

Get Embed code for this video http://www.youtube.com/watch?v=wZy3ZJOxh9E with option autoplay set to true.
echo get_youtube_embed("5P6UU6m3cqk", true);

Posted in PHP | Tagged | 1 Comment

Cut A String by a number of words PHP

Cut_string_by_words function cuts a string by a number of words specified in $num_of_words argument.

function cut_string_by_words($num_of_words, $string)
{
	preg_match("/([\S]+\s*){0,$num_of_words}/", $string, $regs);
	$short_string = trim($regs[0]);
	return $short_string;
}

Sample Code:

$input_string = "Microsoft on Tuesday started rolling out software updates to users of the Samsung Focus and LG Quantum";
echo  cut_string_by_words(8, $input_string);
 
Ouput:
Microsoft on Tuesday started rolling out software updates
Posted in PHP | Tagged , | Leave a comment

PHP remove extra space in a string

The function remove_extra_space removes extra spaces in a string.

/*
*	Remove extra spaces from a string. Multiple spaces will be shortened to one space character.
*	Example: "This is       a  test     string " will be shortened to "This is a test string"
*/
function remove_extra_space($input_string)
{
	return preg_replace('/(\s)\s+/', '\\1', $input_string);
}

If you want to completely remove all space(s) in a string then change then use the function below.

function remove_all_spaces($input_string)
{
	return preg_replace('/(\s)/', '', $input_string);
}
Posted in PHP | Tagged | Leave a comment