Showing posts with label explode. Show all posts
Showing posts with label explode. Show all posts

Saturday, 3 September 2016

Explode a String by Multiple Delimiters in PHP

Explode a string using multiple delimiters in php. multipleExplode function helps in spliting a string by multiple delimiters in php.

function multipleexplode($delimiters,$string) {
    $ready = str_replace($delimiters, $delimiters[0], $string);
    $array = explode($delimiters[0], $ready);
    return  $array;
}

Friday, 26 August 2016

Count Words in a String in PHP

In Some cases we need to calculate or count the number of words in a sentence or a long string.
This simple function takes a string as parameter and counts the number of words in the string based on the space in the string.

function countWords($string) {
   $word_array = explode(" ", $string);
   return count($word_array);
}