Monday, 3 September 2018

PHP: Sort the table by value of occurrences in the text?

I need to order an unknown array of keywords by the order they occur in a text string.

// random order of unknown values
$keywords = array( "orange", "green apple", "banana" );

// ordered occurrences
$text = "This is text where the keywords can appear as well like green apple trees.
Then follows text where the keywords are in order and surrounded by unique syntax.

((randomly ordered keywords))
((foo))
((banana))
((orange))
((baz))
((green apple))
((baz))
";

// reorder keywords
// maybe preg_match( "/.+\({2}(".$keywordItem.")\).*/", $text, $matches )
// ...

// Order should be banana, orange, green apple
print_r( $keywords );

The solution may include ´preg_match`, but I don't know where to start.

  1. First extract the words from the string (I'm assuming each keyword appears in the text in it's own separate line)
    preg_match_all('/[ \t]*\(\((.*)\)\)[ \t]*/', $text, $matches);`
    
    
  2. Then you take the produced array and intersect with the keywords
    if (isset($matches[1])) {
      $results = array_intersect($matches[1], $keywords);
    }
    
    
Here's the complete snippet (php pad):
$keywords = array( "orange", "green apple", "banana" );

$text = "This is text where the keywords can appear as well like green apple trees.
The follows text where the keywords are in order and surrounded by unique syntax.

((randomly ordered keywords))
((foo))
((banana))
((orange))
((baz))
((green apple))
((baz))
";

// extract keywords
$matches = array();
$results = array();
preg_match_all('/[ \t]*\(\((.*)\)\)[ \t]*/', $text, $matches);

// Sort keywords
if (isset($matches[1])) {
  $results = array_intersect($matches[1], $keywords);
}

var_dump($results);

0 comments:

Post a Comment