htmlentities()
This PHP function is used to convert all applicable characters to HTML entities. When you want to display some HTML code in your page it will show the result of your code but not the actual code in that kind of situation you can use htmlentities() and one more function Htmlspecialchars() will be explaining more about this function down.
<?php
$str = "I like <b>PHP</b>";
echo "Actual result:<br>";
echo $str.'<br><br>';
echo "Result by using function:<br>";
echo htmlentities($str);
?>
Result:
Htmlspecialchars()
<?php
$str = "<a href='http://www.phpjavascript.com/'>PHP</a>";
echo "Actual result:<br>";
echo $str.'<br><br>';
echo "Result by using function:<br>";
echo htmlspecialchars($str, ENT_QUOTES);
?>
Result:
strip_tags()
This function is identical to htmlentities() function except strip_tags function strips html tags and html from a string.
<?php
$str = "<a href='http://www.phpjavascript.com/'>PHP</a>";
echo "Actual result:<br>";
echo $str.'<br><br>';
echo "Result by using function:<br>";
echo strip_tags($str, ENT_QUOTES);
?>
Result:
explode()
The explode function take its input as a string and convert it into an array. The function takes the first parameter as separator and second one is a string. It converts a string into an array based on the separator.
<?php
$cars = "BMW,Mercedes,Audi,Honda,Ferrari";
$brands = explode(",", $cars); //separate by comma(,)
echo 'Brand Name : '.$brands[0].'<br>'; //BMW
echo 'Brand Name : '.$brands[2]; //Audi
?>
Result:
implode()
This function is just opposite to explode function. As explode function converts string into array, but the implode function take its input as array and convert it into string.
<?php
$cars = array("BMW","Mercedes","Audi","Honda","Ferrari");
$brands = implode(",", $cars);
echo $brands;
?>
Result:
rand()
The rand() function outputs a random number. there are two optional parameters that you can add to set mi and max for the number.
<?php
echo rand();
echo '<br>';
echo rand(10,20);
?>
Result:
str_replace() This function is useful when you want to do a simple match and replace. This function accepts four parameters, three required. find, replace, string, count(optional).<?php $string = 'aaa-bbb-ccc'; echo str_replace('-', '*', $string); ?>
Result:date() The date() function can have two parameters, format(required) and timestamp(optional). <?php echo date("d-m-Y h:i:s"); ?> Result:
strlen() The strlen function will return the length of a given string. The spaces and characters are added to the count.count() This function count the number of element in a array.<?php $stringOne = "Hello PHP"; echo 'stringOne Length = ' . strlen($stringOne) . '<br><br>'; $stringTwo = " Welcome code"; echo 'stringTwo Length = ' . strlen($stringTwo); ?> Result:
<?php $Alphabets = array("A", "B", "C", "D", "E"); echo 'Count = '.count($Alphabets); ?>
Result: array_combine() This function creates an array by using one array for keys and another for its values. <?php $fruit_color = array("green", "red", "yellow"); $fruits = array("avocado", "apple", "banana"); $fruits_array= array_combine($fruit_color, $fruits); print_r($fruits_array); ?> Result:array_unique() You can use this function to removes duplicate values from an array. <?php $input = array("a" => "green", "red", "b" => "green", "blue", "red"); $result = array_unique($input); print_r($result); ?> Result:
print_r() This function is used to print array variable in a way that’s readable by humans. <?php $fruits = array ("a" => "Banana", "b" => "Apple"); print_r ($fruits); ?> Result:
strrev() This function reverses the given string. <?php echo strrev("Hello PHP"); ?> Result:
trim() This function removes characters, space from the string form both the ends. <?php $str = "Hello PHPJavaScript"; echo $str . "<br>"; echo trim($str,"Ht"); ?> Result:
ucfirst()
This function Converts the first character to uppercase. <?php echo ucfirst("hello php!"); ?> Result:
For all PHP built in functions, go to PHP.NETPlease do share and fallow our blog for more intresting content.
No comments:
Post a Comment