1

I have a piece of PHP code as follows:

$words = array(
            'Artist' => '1',
            'Tony' => '2',
            'Scarface' => '3',
            'Omar' => '4',
            'Frank' => '5',
            'Torrentino' => '6',
            'Mel Gibson' => '7',
            'Frank Sinatra' => '8',
            'Shakes' => '9',
            'William Shakespeare' => '10'
    );
$text = "William Shakespeare is very famous in the world. An artist is a person engaged in one or more of any of a broad spectrum of activities related to creating art, practicing the arts, and/or demonstrating an art. Artist is a descriptive term applied to a person who engages in an activity deemed to be an art. Frank Sinatra was an American singer, actor, director, film producer, and conductor. Frank Sinatra was born on December 12, 1915, in Hoboken, New Jersey, the only child of Italian immigrants Natalina Garaventa and Antonino Martino Sinatra, and was raised Roman Catholic.";

$re = '/\b(?:' . join('|', array_map(function($keyword) {
    return preg_quote($keyword, '/');
}, array_keys($words))) . ')\b/i';

preg_match_all($re, $text, $matches);
foreach ($matches[0] as $keyword) {
    echo $keyword, " ", $words[$keyword], "\n";
}

The code returns the below:

William Shakespeare 10 artist Artist 1 Frank 5 Frank 5

The code works well to not echo the partial keywords such as 'Shakes' => '9' in Shakespeare. However, as you can see the code can not detect 'Frank Sinatra' => '8' as a keyword as it is in Frank Sinatra was an American singer, also artist does not have any value (i.e. 1). Could you please help me to change the code somehow to echo William Shakespeare 10 artist 1 Artist 1 Frank 5 Frank 5 Frank Sinatra 8 Frank Sinatra 8 instead of William Shakespeare 10 artist Artist 1 Frank 5 Frank 5 in the current version. thanks for your help.

2
  • I don't see any problem here. Output for your code is William 9 World Cup 4 William Shakespeare 8 and not World Cup 4 William Shakespeare 8 so it seems it works as you want Commented Jul 6, 2014 at 18:52
  • @MarcinNabiałek, thank you Marcin, could you please review the question again? Commented Jul 6, 2014 at 19:43

1 Answer 1

2

I've managed to achieve result:

William Shakespeare 10 artist 1 Artist 1 Frank Sinatra 8 Frank Sinatra 8

using the code:

<?php

mb_internal_encoding('UTF-8');
$words = array(
            'Artist' => '1',
            'Tony' => '2',
            'Scarface' => '3',
            'Omar' => '4',
            'Frank' => '5',
            'Torrentino' => '6',
            'Mel Gibson' => '7',
            'Frank Sinatra' => '8',
            'Shakes' => '9',
            'William Shakespeare' => '10'
    );


uksort($words, function ($a, $b) {
    $as = mb_strlen($a);
    $bs = mb_strlen($b);

    if ($as > $bs) {
        return -1;
    }
    else if ($bs > $as) {
        return 1;
    }
    return 0;


});

$words_ci = array();

foreach ($words as $k => $v) {
    $words_ci[mb_strtolower($k)] = $v;
}

$text = "William Shakespeare is very famous in the world. An artist is a person engaged in one or more of any of a broad spectrum of activities related to creating art, practicing the arts, and/or demonstrating an art. Artist is a descriptive term applied to a person who engages in an activity deemed to be an art. Frank Sinatra was an American singer, actor, director, film producer, and conductor. Frank Sinatra was born on December 12, 1915, in Hoboken, New Jersey, the only child of Italian immigrants Natalina Garaventa and Antonino Martino Sinatra, and was raised Roman Catholic.";

$re = '/\b(?:' . join('|', array_map(function($keyword) {
    return preg_quote($keyword, '/');
}, array_keys($words))) . ')\b/i';



preg_match_all($re, $text, $matches);
foreach ($matches[0] as $keyword) {
    echo $keyword, " ", $words_ci[mb_strtolower($keyword)], "\n";
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.