Tag Archives: validation function

PHP: What is the difference between split and explode php?

Difference between split and explode php

Do you know what is the main difference between split() and explode()?

No worries 🙂 , the main difference in split() and explode is the way, it uses to splitting a large string.


Both the functions are used to Split a string. However, Split is used to split a string using a regular expression. On the other hand, Explode is used to split a string using another string.


The split() function splits the string into an array using a regular expression and returns an array.
Ex: split (:India:Nepal:Srilanka); returns an array that contains India, Nepal, Srilanka.


The explode () function splits the string by string.
Ex: explode (and India and Nepal and Srilanka); returns an array that contains India, Nepal, Srilanka.


Have a look by below example.

split (":", "i:am:reading:scriptarticle.com");

returns an array that contains i,am,reading,scriptarticle.com.



The explode () function splits the string by string.

explode ("I", "I am reading scriptarticle.com");

returns an array that contains

array (
0 => ”,
1 => ‘ am reading scriptarticle.com’,

Note: split () function has been DEPRECATED as of PHP 5.3.0.


It’s been deprecated because


  • explode() is substantially faster because it doesn’t split based on a regular expression, so the string doesn’t have to be analyzed by the regex parser
  • preg_split() is faster and uses PCRE regular expressions for regex splits


join() and implode() are aliases of each other and therefore don’t have any differences.


Hope it helps!!

Functionality of PHP trim() function


trim — Strip whitespace (or other characters) from the beginning and end of a string.



string trim ( string $str [, string $charlist ] )



The string that will be trimmed.

Optionally, the stripped characters can also be specified using the charlist parameter.


Argument Options:

PHP trim function returns a string with white space stripped from the beginning and end of str. Without the second parameter, trim() will strip these characters:


” ” (ASCII 32 (0x20)), an ordinary space
“\0” (ASCII 0 (0x00)), the NUL-byte
“\n” (ASCII 10 (0x0A)), a new line (line feed)
“\t” (ASCII 9 (0x09)), a tab
“\r” (ASCII 13 (0x0D)), a carriage return
“\x0B” (ASCII 11 (0x0B)), a vertical tab


Can I trim other characters than these?

Yes, this is possible and easy, you just use a second parameter with all characters you want to trim.


Trim these characters from the beginning and end of a string: “S”,”c”,”e”.

<pre>$mystr = trim ( "Scriparticle" , "Sce") ;
echo $mystr;

Result will



Useful PHP validation functions

String Validation
Validate name, surname by the below function to restrict only letters and spaces.

function validateString($str)
//accept letters and space only
return preg_match('/^[A-Za-zs ]+$/', $str);

Validate numeric value by the below function to restrict only numbers,you can uncomment any line according to your need.

function validateNumber($value)
return is_numeric($value);

Alphanumeric Characters Validation
Below function validates alphanumeric characters.ctype is complete function library in PHP.

function validateAlphanumeric($string)
return ctype_alnum ($string);

Validation for URL Exist or not
This function will check whether a given URL exist and not only validate it.

function urlExist($url)
$url = @parse_url($url);
if (!$url){
return false;
$url = array_map('trim', $url);
$url['port'] = (!isset($url['port'])) ? 80 : (int)$url['port'];
$path = (isset($url['path'])) ? $url['path'] : '';

if ($path == ''){
$path = '/';
$path .= (isset($url['query'])) ? '?$url[query]' : '';
if (isset($url['host']) AND $url['host'] != @gethostbyname($url['host']))
if (PHP_VERSION >= 5){
$headers = @get_headers('$url[scheme]://$url[host]:$url[port]$path');
$fp = fsockopen($url['host'], $url['port'], $errno, $errstr, 30);
if (!$fp){
return false;
fputs($fp, 'HEAD $path HTTP/1.1rnHost: $url[host]rnrn');
$headers = fread($fp, 4096);
$headers = (is_array($headers)) ? implode('n', $headers) : $headers;
return (bool)preg_match('#^HTTP/.*s+[(200|301|302)]+s#i', $headers);
return false;

Validation for check Image Exist
Below function will check whether a given image link exist and not.

function imageExist($url) {
if(@file_get_contents($url,0,NULL,0,1)){return 1;}else{ return 0;}

IP Address Validation
function will validate an IP address.

function validateIP($IP){
return preg_match('/^(([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5]).){3}([1-9]?[0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/',$IP)

Validation for Proxy
This function will detect proxy visitors even those that are behind anonymous proxy 🙂

function validateProxy(){
|| in_array($_SERVER['REMOTE_PORT'], array(8080,80,6588,8000,3128,553,554))
|| @fsockopen($_SERVER['REMOTE_ADDR'], 80, $errno, $errstr, 30))
exit('Proxy detected');

Strong Password Validation
Check whether a particular password filled by the user is strong enough or not.

function validatePassword($password){
//must contain 8 characters, 1 uppercase, 1 lowercase and 1 number
return preg_match('/^(?=^.{8,}$)((?=.*[A-Za-z0-9])(?=.*[A-Z])(?=.*[a-z]))^.*$/', $password);

Credit Card Validation
Below function validate credit card number format.

function validateCreditCard($cc){
//eg. 746486033718121
return preg_match('/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|6011[0-9]{12}|3(?:0[0-5]|[68][0-9])[0-9]{11}|3[47][0-9]{13})$/', $cc);

JavaScript function equivalent to PHP’s str_replace

Below is the JavaScript function worked like PHP’s str_replace.

<script type="text/javascript">
function str_replace (search, replace, subject)
var result = "";
var  oldi = 0;
for (i = subject.indexOf (search); i > -1; i = subject.indexOf (search, i))
result += subject.substring (oldi, i);
result += replace;
i += search.length;
oldi = i;
return result + subject.substring (oldi, subject.length);


You can find more equivalent JavaScript function same as it works for PHP.


You can get the library of these functions by php.js or can directly download by below link.

Download php.default.min.js


PHP email validation function & file extension validation function

Very easy and handy to use PHP email validation function and File type validation function/methods.

Email validation


* Function for email validation
* @param string $email
* @return boolean

function valid_email($email) {
$result = true;
if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,4})$", $email)) {
$result = false;
return $result;


Image File extension validation


* Function for checking the image (by extension)
* @param string $file_name
* @return boolean

function is_image_file($file_name)
$arr_img = explode('.',$file_name);
$image_extension = end($arr_img);
//    array of all the allowed extensions
$image_extensions_allowed =array('jpg','jpeg','png','gif','JPG','JPEG','PNG','GIF');
return true;
return false;


read out some more ready to use PHP validation functions/methods.

PHP function to get current web page full URL

PHP function to get current web page full URL & current page name.

Sometimes, you might want to get the current page full URL. Here is way you can do that.

* Function for get the full current website page URL
* @return string(full url of current website page)

function fullPageUrl()
$pageURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
if ($_SERVER["SERVER_PORT"] != "80"){
} else {
return $pageURL;


Get current page name.

* Function for just getting the current website page name
* @return string

function curPageName() {
return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);

Hope these functions will help you, leave comments if you found anything wrong  or these functions is not working on your page.