Archive for the ‘Php’ Category
Bing Translator – PHP Usage
I’d like to share a php code that you can use for bing translator api. Just add your bing api id into “AppID= ” part.
<?php
echo translate(’Hello World’, ‘en’, ‘fr’);
function translate($text, $from, $to) {
$data = file_get_contents(’http://api.bing.net/json.aspx?AppId=YoutApiIDHere&Sources=Translation&Version=2.2&Translation.SourceLanguage=’ . $from . ‘&Translation.TargetLanguage=’ . $to . ‘&Query=’ . urlencode($text));
$translated = json_decode($data);
if (sizeof($translated) > 0) {
if (isset($translated->SearchResponse->Translation->Results[0]->TranslatedTerm)) {
return $translated->SearchResponse->Translation->Results[0]->TranslatedTerm;
} else {
return false;
}
} else {
return false;
}
}
?>
Removing folder with the files inside with php
Another usefull php function. It removes a folder with the content inside.
//function
function delTree($dir) {
$files = glob( $dir . ‘*’, GLOB_MARK );
foreach( $files as $file ){
if( substr( $file, -1 ) == ‘/’ )
delTree( $file );
else
unlink( $file );
}
rmdir( $dir );
}
//usage
delTree(’FolrderName’);
php function against sql injection
I’ve began to use this usefull function against sql injection and cleaning other bad characters before inserting data into database.
//function
function clean($str) {
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
// usage:
clean($_POST["data"]);
hope it helps.
