Decode HTML entities from the Strings

Sometimes we need to decode HTML entities from any specific strings in PHP. Basically it’s more used for retrieve HTML entities from a MySQL cell.? To Convert all HTML entities to their applicable characters there is one function available that is called html_entity_decode().

html_entity_decode($YourStrings)

Example:

<?php
$orig?=?"I'll?"love"?the?<b>girl</b> very much";

$a?=?htmlentities($orig);

$b?=?html_entity_decode($a);

echo?$a;?//?I'll?&quot;love&quot;?the?&lt;b&gt;girl&lt;/b&gt; very much

echo?$b;?//?I'll?"love"?the?<b>girl</b> very much?>

apostrophe s ('s) problem in MySQL query

When you will work with your Blog or something in PHP, MySQL. Then your user may enter apostrophe s (‘s) into the post writing area. Then it will cause a problem for executing your MySQL Query. To execute your query for MySQL you can use mysql_real_escape_string($yourStrings).

Example:

<form method=”post” action=””>
<input type=”text” name=”user_name” value=”wallpaperama’s”>
<br />
<br />
<input type=”submit” name=”Submit” value=”Submit”>
</form>

<?php
$user_name = mysql_real_escape_string ($_POST[‘user_name’]);
?>

and write your mysql query. It will be executed without any error. It’s more secured to post data to MySQL database.

For know more details please go to mysql_real_escape_string() in PHP Function Manual.

[adsense_id=”2″]

Check existence of a PHP Function in PHP

Sometimes when you make many functions in your PHP scripts. Then you need to check the real existence of the function you need. That’s why to check a function’s existence, you can write a little piece of code like the following:

<?php
if(function_exists(NameOfTheFunction))
{
echo "Function Exist";
}
else
{
echo "Function doesn't exist";
}
?>

NameOfTheFunction means your function's name. If your function is LoginCheck(). Then you need to write only LoginCheck. Just omitt the () symbol.

If it is helpful for you please make comments and share it.

[adsense_id="2"]

How to write in a .txt file with PHP

To write values or text in a .txt file you can follow the following process in PHP.

<?php

function test_function ()
{
return ?"Hello!";
}

$function_contents = test_function ();

$file_name = "test_file.txt";
$file_handle = fopen($file_name, 'w');
fwrite($file_handle, $function_contents);
fclose($file_handle);

?>

It will make a text file named test_file.txt and in that file this PHP code snippet will write ‘Hello’.

Thanks