Future date in php

24th December
strtotime — It will parse any English textual datetime into a timestamp and will return a non zero value on failure.
Syntax
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
strtotime ($time , $now ); //$time is a string containing an
                           English date format.
//$now is timestamp which is used as
a base for the calculation of relative dates.
echo strtotime("now");
// returns current timestampecho strtotime("10 October 2011")
// returns timestamp for 10 October 2011

echo strtotime("+1 day");
//returns current timestamp+ 1 day(24 hrs)

echo strtotime("+1 week");
// returns current timestamp+ 1 week(7 days)

echo strtotime("+2 week 5 days 23 hours 56 seconds");

echo strtotime("next Friday");

echo strtotime("last Monday");

echo strtotime(“10/12/2011”);
// If we use slashes then strtotime will follow m/d/y

echo strtotime(“10-12-2011”);
// Use – or . for d-m-y format

How to Censor dirty words ?

24th December
How to find any string containing bad/foul words ?
Use the following function to find bad words in a string.
1
2
3
4
5
6
7
8
9
10
11
12
<?php
    // It returns 1 if found any bad words in $texto (Your String to checked).
    // Pass Your Bad Words Separated by comma in $badwords in a string
    // If $spider is true then your string is returned with dirty/bad/foul words replaced by 1
    // If dirty words by some other character instead of 1 then replace $spider variable with your desired string at line 10
    function filter_bad_words($texto, $badwords ,$spider = false){            
        $f = explode(',', $badwords);
        $f = array_map('trim', $f);
        $filtro = implode('|', $f);    
        return ($spider) ? preg_replace("#$filtro#i", $spider, $texto) : preg_match("#$filtro#i", $texto) ;
        }
?>

Remove trailing comma, or any other character.

24th December
If you want to remove trailing character from any string then use the following function.
Example
1
2
3
4
5
6
7
8
<?php
    $test = "php,spider,blog,trailing,comma,string,";
    echo $test."<br>";
    //Output :- php,spider,blog,trailing,comma,string,
    echo rtrim($test,",");
    //Output :- php,spider,blog,trailing,comma,string
    // , in rtrim replace by your trailing character.
?>

How to Translate Text to desired language ?

10th August
Just follow these steps and copy & paste the code you can translate text easily.
Step 1 : Make a div with id (id is compulsory) in Body Tag of your page
Like

1
<div id="content">Loading...</div>
Step 2 : Paste the following code to head section under script tag of your webpage.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<script type="text/javascript" language="javascript">
google.load("language", "1");

function initialize() {
    var content = document.getElementById('content');
    // Setting the text in the div.
    content.innerHTML = '<div id="text">Place Your Text Here</div><div id="translation"/>';

    // Grabbing the text to translate
    var text = document.getElementById("text").innerHTML;

    // Translate from Spanish to English, and have the callback of the request
    // put the resulting translation in the "translation" div.
    // Note: by putting in an empty string for the source language ('es') then the translation
    // will auto-detect the source language.
    google.language.translate(text, 'es', 'en', function(result) {
        var translated = document.getElementById("translation");
        if (result.translation) {
            translated.innerHTML = result.translation;
        }
    });
    }
    google.setOnLoadCallback(initialize);
</script>
Step 3 : And the final Step is paste the following URL in src of script tag.

http://www.google.com/jsapi?key=AIzaSyA5m1Nc8ws2BbmPRwKu5gFradvD_hgq6G0

Above example is translating Spanish to English You can translate as your choice.
Just change the code in
google.language.translate(text, ‘es’, ‘en’, function(result)
es stands for Spanish,
en stands for English
You can find more language code at Click Here
 

How to Remove Characters Except Numbers from a String ?

2nd August

Following Code will remove all character,special characters including space.

1
2
3
4
5
6
7
8
9
10
11
<?php

$givenNumber = "1245fdfd8454D FDFDF434 3$#$#%";

$testingNumber = preg_replace('[D]', '', $givenNumber);

echo $testingNumber;

Output is : 124584544343

?>

Difference between strstr() and stristr() functions in PHP.

2nd August

Both functions are used to return or finds the first occurence of a substring from a string, and give all string from first occurence to end of string except than stristr() is case-insensitive.
If no match is found then FALSE will be returned.
Example

1
2
3
4
5
6
7
8
<?php
$email = 'abc@xyz.com';
$host = strstr($email, '@');
echo $host;

output: @xyz.com
stristr() does the same thing in Case-insensitive manner
?>

How to Find Quarter Month From Any Year ?

2nd August

A Quarter means 1/4 part of year.
In the following example given Script is
use to find which quarter of the year
in the given date.

Assuming given date is 10 December 2015
Following is the Example.
Just Copy & Paste and Enjoy.

1
2
3
4
5
6
7
<?php
  $month = 12;
  $date = 10;
  $year = 2015;
  $qm = mktime(0,0,0,$month,$date,$year);
  echo ceil(date("m", $qm)/3);
?>

Validate Email in PHP

19th May

Use this function to validate email in PHP.

Pass your email in this function by calling it.if  it returns false then email is not valid else valid.

1
2
3
4
5
6
7
8
9
10
<?php
function checkEmail($email_address = null){
if(preg_match("/^[a-zA-Z]*w+(.w+)*@w+(.[0-9a-zA-Z]+)*.[a-zA-Z]{2,4}$/", $email_address) === 0){
return false;
}
else{
return true;
}
}
?>

How to share your page or website ?

14th April
1
2
3
4
5
6
7
8
9
10
11
12
13
<!-- AddToAny BEGIN -->
<div>
<a href="http://www.addtoany.com/share_save?linkurl=url here&amp;amp;linkname=">Share</a>
<span class="a2a_divider"></span>
<a class="a2a_button_twitter"></a>
<a class="a2a_button_facebook"></a>
</div>
<script type="text/javascript">
var a2a_config = a2a_config || {};
a2a_config.linkurl = "url her";
</script>
<script type="text/javascript" src="http://static.addtoany.com/menu/page.js"></script>
<!-- AddToAny END -->

Validate URL in PHP

8th March

To validate url at server side use this function.
In this function $variable defines the your string which you want to validate.

1
2
3
4
<?php
$reg_exp = /(http?://)?([da-z.-]+).([a-z.]{2,6})([/w ?=.-]*)*/?$/
ereg($reg_exp,$variable);
?>