Showing posts with label PHP IMPLODE. Show all posts
Showing posts with label PHP IMPLODE. Show all posts

Wednesday, 3 June 2015

Monday, 23 February 2015

PHP: Highlight specific words in a phrase

Sometimes, for example, when displaying search results, it is a great idea to highlight specific words. This is exactly what the following function can do:

<?php

function highlight($sString, $aWords) {

 if (!is_array ($aWords) || empty ($aWords) || !is_string ($sString)) {
  return false;
 }

 $sWords = implode ('|', $aWords);
  return preg_replace ('@\b('.$sWords.')\b@si', '<strong style="background-color:yellow">$1</strong>', $sString);
}
?>

Saturday, 27 September 2014

implode in PHP

PHP implode() function is utilized to returns a string formed from the elements of an array.
PHP implode() function is easily remembered as "array to string", which simply means that it takes an array and returns a string.


Syntax:
implode(separator,array)

Parameters description:
separator : Optional. Connector between the array elements.Default is empty string(“ ”).

array : Required. The array who's elements will be joined.

Example:


<?php

$array_name = array('Today','is','Monday!');
$str_frm_array = implode(" ",$array_name);
echo $str_frm_array;
?>
The output will be:
Today is Monday!