Category Archives: PHP

How to Translate Text to desired language ?

Just follow these steps and copy & paste the code you can translate text easily.

Step 1 : Make a div with id (id is compulsory)
Like <div id="content">Loading…</div>

Step 2 : Paste the following code to head section under script tag of your webpage.
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 (Hola, me alegro mucho de verte.)<\/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);

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 http://code.google.com/apis/language/translate/v1/reference.html


How to Remove Characters Except Numbers from a String ?

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

$givenNumber = “1245fdfd8454D FDFDF434 3$#$#%”;

$testingNumber = preg_replace(‘[\D]’, ”, $givenNumber);

echo $testingNumber;

Output is : 124584544343


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

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

$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 ?

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.

$month = 12;
$date = 10;
$year = 2015;
$qm = mktime(0,0,0,$month,$date,$year);
echo ceil(date(“m”, $qm)/3);