Tuesday, November 12, 2013

Flip multidimensional array in PHP

In this post you learn that how to flip a multidimensional array in php. Below function to accomplish the your task.
<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

function multi_array_flip($arrayIn, $DesiredKey, $DesiredKey2=false, $OrigKeyName=false) { 
$ArrayOut=array(); 
foreach ($arrayIn as $Key=>$Value) 
    { 
        // If there is an original key that need to be preserved as data in the new array then do that if requested ($OrigKeyName=true) 
        if ($OrigKeyName) $Value[$OrigKeyName]=$Key; 
        // Require a string value in the data part of the array that is keyed to $DesiredKey 
        if (!is_string($Value[$DesiredKey])) return false; 

        // If $DesiredKey2 was specified then assume a multidimensional array is desired and build it 
        if (is_string($DesiredKey2)) 
        { 
            // Require a string value in the data part of the array that is keyed to $DesiredKey2 
            if (!is_string($Value[$DesiredKey2])) return false; 

            // Build NEW multidimensional array 
            $ArrayOut[$Value[$DesiredKey]][$Value[$DesiredKey2]]=$Value; 
        } 

            // Build NEW single dimention array 
        else $ArrayOut[$Value[$DesiredKey]][]=$Value; 
    } 
return $ArrayOut; 
}//end multi_array_flip 

?>

Monday, November 4, 2013

Regular Expression in PHP


In this post you learn that how to write the Regular Expression. Regular expression is the most important part in form validations and it is widely used for search, replace and web crawling systems. If you want to write a selector engine (used to find elements in a DOM), it should be possible with Regular Expressions. 



Basics of regular expression in following three parts.

PART 1

^     Start of string
$     End of string
.      Any single character
+     One or more character
\      Escape Special characters
?    Zero or more characters



<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

#Input exactly match with “abc” 
$A = /^abc$/;

#Input start with “abc”
$B = /^abc/;

#Input end with “abc”
$C = /abc$/;

#Input “abc” and one character allowed Eg. abcx
$D = /^abc.$/;

#Input  “abc” and more than one character allowed Eg. abcxy
$E = /^abc.+$/;

#Input exactly match with “abc.def”, cause (.) escaped
$F = /^abc\.def$/;

#Passes any characters followed or not by “abc” Eg. abcxyz12....
$G = /^abc.+?$/


?>




PART 2

[abc]               Should match any single of character
[^abc]             Should not match any single character
[a-zA-Z0-9]    Characters range lowercase a-z, uppercase A-Z and numbers
[a-z-._]            Match against character range lowercase a-z and ._- special chats 
(.*?)                 Capture everything enclosed with brackets 
(com|info)       Input should be “com” or “info”
{2}                    Exactly two characters
{2,3}                 Minimum 2 characters and Maximum 3 characters
{2,}                   More than 2 characters

<?php
/*
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

#URL validation:

var URL = /^(http|https|ftp):\/\/(www+\.)?[a-zA-Z0-9]+\.[a-zA-Z0-9]+\.([a-zA-Z]{2,3})\/?/;
URL.test(“http://4evertutorials.blogspot.com”); // pass
URL.test(“http://www.4evertutorials.blogspot.com”); // pass
URL.test(“https://4evertutorials.blogspot.com/”); // pass

?>

PART 3

\d is short form of [0-9]  Any numbers
\D is short form of [^0-9] Any non-digits
\w is short form of [a-zA-Z0-9_] Characters,numbers and underscore
\W is short form of [^a-zA-Z0-9_] Except any characters, numbers and underscore
\s White space character
\S Non white space character


 

© 2014 4everTutorials. All rights resevered.

Back To Top