Create Keyword Lists from Text in PHP

Create Keyword Lists from Text in PHP

This post shows you how to use array functions to make a keyword list from text. First, let's look at two useful array functions: explode() and array_unique().

explode function

The explode function splits a string by a string.

$str = 'It is 6 pm now';
$data = explode(' ', $str);
print_r($data);

In the above example, the explode function has two parameters, the separator: a space and the input string: $str. The input string $str will be split into five array elements by space. The output will be:

 Array ( [0] => It [1] => is [2] => 6 [3] => pm [4] => now )

array_unique function

The array_unique function returns a new array with unique values from the input array.

$a = array("dog", "cat", "cat", "bird", "bird");
print_r(array_unique($a, SORT_STRING));

The second parameter of array_unique() may be used to define the sorting behaviour. Here, SORT_STRING is for comparing items as strings. The above example will output:

Array ( [0] => dog [1] => cat [3] => bird )

Extract keywords from text

As shown in the code snippet below, the function getKeywords() extracts keywords from an input string.

function getKeywords($str) {

    //convert into lowercase
    $str = strtolower($str);

    //remove punctuation characters
    $str = preg_replace('/[.,\/#!$%\^&\*;:{}=\-_`~()\[\]]/', '', $str);

    //return an array of the string
    $data = explode(' ', $str);

    $result = array_unique($data, SORT_STRING);
    return $result;
}

The function strtolower() converts all characters to lowercase. Then, punctuation characters are removed by using the regular expression search and replace function preg_replace(). After that, the words in the string $str are split and stored in a new array $data. Finally, the keywords of the input string are extracted and returned.

To test the function getKeywords(), we can do like this:

$str = 'There are a cat, a dog, and a zebra.';
$keywords = getKeywords($str);
print_r($keywords);

The output will be:

Array ( [0] => there [1] => are [2] => a [3] => cat [5] => dog [6] => and [8] => zebra )

Thanks for reading! To find more programming tutorials, please visit: CodeBilby.com

Did you find this article valuable?

Support Yan's Blog by becoming a sponsor. Any amount is appreciated!