Implode and Explode in PHP

We can store multiple value in single variable in two ways.
Either using Array, or comma separated values.

Comma separated: 
$name = 'Alex,Ana,BOB';
Array:  
$name =  array(
    [0] => 'BOB',
    [1] => 'Alex',
    [2] =>  'Ana'
);

To convert array to comma separated value and reverse
we required two function

implode : 
Syntax:  string implode ( string $glue , array $pieces )

$array = array(
    [0] => 'lastname',
    [1] => 'email',
    [2] => 'phone'
);
$comma_separated = implode(",", $array);

result :   will be "lastname,email,phone".
explode : 
Syntax:  array explode (string $delimiter,string $comma_string)

$input2 = "hello,there";
$result = var_dump( explode( ',', $input2 ) );

result : array( 
    [0] => 'hello',
    [1] => 'there'
);

Thanks ……!

Crop image script

So you have a wordpress website and you have a form where user enters data, you have given an ‘upload image’ option but you want the uploaded images to be cropped for proper display ?
Below is the code that might help you crap crop it.

How to use it

Just paste the above code in functions.php file of your active wordpress theme. Then call the function wdw_crop_img passing proper parameters.

The above function out of the box is ‘wordpress specific’ because line no 23 in the code makes use of wordpress core function to get to the uploads directory (check line no 23, 25, 26 in the code). But you can obviously modifyl those 3 lines to suit to your website’s directory structure and make it work on any non-wordpress php website.

Rest, read the comment section above function declaration to figure out about the parameters passed and output.

Drop in your comment if you find any problem.
That’s it in here. Hope you find it useful.

Generate youtube embed code from url

php Code to convert Youtube url into embed code. Pass in the youtube video url and generate the embed code to embed the youtube video on your website.

Without talking any more, here’s the php code:

The main method youtubeEmbedFromUrl uses helper methods extractUTubeVidId (description here) and generateYoutubeEmbedCode

How to use:

Just call the method youtubeEmbedFromUrl with 3 parameters:

  1. youtube_url : the url of the youtube video
  2. width : desired width of the youtube player
  3. height : desired height of the youtube player

e.g:

<?php
$embed_code = youtubeEmbedFromUrl("http://www.youtube.com/watch?v=9Jr6OtgiOIw", 400, 300);
echo $embed_code ;
?>

That’s it here. Hope you find it useful.

Will soon update a c# version of the code

Get youtube video id from url

Extract you tube video id from youtube url. The following code retrieves the video id from youtube url.

It works with only 3 types of youtube url supplied:
Type1: http://www.youtube.com/watch?v=9Jr6OtgiOIw
Type2: http://www.youtube.com/watch?v=9Jr6OtgiOIw&feature=related
Type3: http://youtu.be/9Jr6OtgiOIw

type1 and 2 is acutally one and the same, only the extra paramters

The code:

How to use:

call this function passing a youtube url as parameter:

<?php
$youtube_video_id = extractUTubeVidId("http://www.youtube.com/watch?v=9Jr6OtgiOIw");
?>

You can use it on your website: user enters youtube url, you pull out the video id, generate an embed code and embed the youtube video on your website.

That’s it here. Hope you find it useful.
You might as well like the code for generating youtube embed code

Will soon update a c# version of the code

how to highlight the current page in menu in php

This is one of the first problems faced by new-by developers when moving to php from static html pages. How to highlight the current page in menu? How to highlight the current page navigation in header.php?

In a HTML website you have separate pages and in each page you assign a css class of ‘active’ or ‘current’ to the link of current page in main menu. Now, in php you split the entire html into different pages like header.php, page.php, footer.php etc.. So your navigation goes into header.php. The question comes how to recognize in header.php that which page is being viewed?

A simple way to do it is:

1) Declare a variable in your pages(e.g: aboutus.php, cotactus.php) just before including header.php or menu.php whichever file your navigation markup is in.

<?php
$page = 'aboutus';
include('header.php');
?>

/*rest of your page content follows */

2) Now in your header.php you read the value of this variable to determine which is the page being viewed and accordingly you assign a different/additional css style to its navigational link.

<ul class="mainmenu" >
	<li class="menuitem <?php if($page=='aboutus'){echo 'active';}?>" >
		<a href="aboutus.php"><span>About Us</span></a>
	</li>
	<li class="menuitem <?php if($page=='contactus'){echo 'active';}?>" >
		<a href="contactus.php"><span>Contact Us</span></a>
	</li>  
	
</ul>

Hope it helps…