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


Thursday, September 5, 2013

Set php.ini file Values Using .htaccess


If are using PHP as an apache module, PHP allows us to modify php.ini using Apache Configuration. This can be done using the following directives:

php_value name value 
Sets the value of the specified directive. Can be used only with PHP_INI_ALL and PHP_INI_PERDIR type directives. To clear a previously set value use none as the value.

php_flag name on|off
Used to set a boolean configuration directive. Can be used only with PHP_INI_ALL and PHP_INI_PERDIR type directives.

php_admin_value name value
Sets the value of the specified directive. This can not be used in .htaccess files. Any directive type set with php_admin_value can not be overridden by .htaccess or ini_set(). 

php_admin_flag name on|off 
Used to set a boolean configuration directive. This can not be used in .htaccess files. Any directive type set with php_admin_flag can not be overridden by .htaccess or ini_set().

In .htaccess File put following code

#htacces code format
php_value setting_name setting_value

#example
php_value  upload_max_filesize  7M


Of course you could simply place these in the .htaccess
It's actually very easy

Check domain availability using cURL php

Here is a simple function for checking domain availability using cURL in php.


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

function domain_checker($domain) { 

$data = 'http://'.$domain; 

// Create a curl handle to a non-existing location 
$ch = curl_init($data); 

// Execute 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_exec($ch); 

// Check if any error occured 
if(curl_errno($ch)) 
{ 
    return 'The domain is available!'; 
} else { 
    return 'The domain is not available'; 
} 

// Close handle 
curl_close($ch); 
} 


?>



// Usage: 

<?php
domain_check("blogger.com");

?> 

WordPress Functions for functions.php File

Here are some of common functions in WordPress theme’s functions.php file that I would suggest you include in your functions.php file



Get Ping/Trackback Count

Here is an interesting one I used recently. This function returns the number of pings/trackbacks for a post. This can be useful if you only want to show a certain section if there are any pings/trackbacks.
<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

function pings_count($post_id) {
    global $wpdb;
    $count = "SELECT COUNT(*) FROM $wpdb->comments WHERE (comment_type = 'pingback' OR comment_type = 'trackback') AND comment_post_ID = '$post_id'";
    return $wpdb->get_var($count);
}


?>


Plain Text Feedburner Subscriber Count

Here is the classic: get your feedburner subscriber count in plain text. Note most of these functions make use of cURL and this one requires PHP v5+ (so you can use SimpleXMLElement).

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

function get_subscriber_count() {
        $id = "YourFeedId";
    $url="https://feedburner.google.com/api/awareness/1.0/GetFeedData?uri=". $id;
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    $data = curl_exec($ch);
    curl_close($ch);
    $xml = new SimpleXMLElement($data);
    return $xml->feed->entry['circulation'];
}


?>

Get TinyURL

This one can be useful if you want to generate a short URL of your post for people to share.
<?php
/* 
Online PHP Examples with Source Code
website: http://4evertutorials.blogspot.in/
*/

function get_tiny_url($url) {
    if (function_exists('curl_init')) {
        $url = 'http://tinyurl.com/api-create.php?url=' . $url;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        $tinyurl = curl_exec($ch);
        curl_close($ch);
        return $tinyurl;
    } else {
        //cURL disabled on server; Return long URL instead.
        return $url;
    }
}


?>

Month Number to Month Name PHP


This is a simple code for you to convert a month number to a month name in PHP 

Example:


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

$monthNum = 11;
$monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
echo $monthName; //output: November




?>

Saturday, August 24, 2013

Calendar Script PHP

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



$day = date('j'); //what day is it today
$month = date('n'); //what month are we in?
$year = date('Y'); //what year are we in?

//get the first first day of the month
$first_day_of_month = date('w',mktime(1,1,1,$month,1,$year));

$days_in_month = date('t'); //how many days in this month

print "SU\tMO\tTU\tWE\tTH\tFR\tSA
"; //print the weekday headers //count ahead to the weekday of the first day of the month for ($spacer = 0; $spacer < $first_day_of_month; $spacer++) { print "  \t"; } for ($x = 1; $x <= $days_in_month; $x++) { //begin our main loop //if we have gone past the end of the week, go to the next line if ($spacer >= 7) { print "
"; $spacer = 0; } //if the length of the current day is one, put a "0" in front of it if (strlen($x) == 1) { $x = "0$x"; } if ($x == $day) { //is this day the current day print "$x\t"; //if so put an indicator } else { print "$x\t"; //otherwise just print it } $spacer++; //increment our spacer } ?>

 

© 2014 4everTutorials. All rights resevered.

Back To Top