0

For a news script I want to recognize the content the user filled in about artists. So if they mention an artist I want to put this in an external database and when somebody views the article they can see the related artists that comes with the article they're reading. But I'm having a sort of creativity problem which holds me back. I use a explode function to seperate the words:

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

$pieces = multiexplode(array(",",".","|",":","<",">"," "), $_POST['article_content']);

So if I have mentioned the artist Afrojack for example I can find it back in the $pieces array. Works well but found a problem. If for instance the user mentioned Nicky Romero, the artist is spread over two arrays and I can't possibly find it in my database filled with artists. So I thought of looking through two words, three words and everything but there has to be simple way I think. So my question is, how do I fix the problem that the script can only find oneword artists.

Bart Scheffer
  • 497
  • 1
  • 3
  • 18
  • from what i understand use `preg_match_all()` – Dev Man Jul 27 '14 at 17:46
  • How are you supposed to know which word pairs or groups coincide with an artist name? Do you have a list already, or are you relying on your split method's results? – Jared Farrish Jul 27 '14 at 18:06
  • I'm actually looking in to the function of it but I can't figure out how to use this one with my problem – Bart Scheffer Jul 27 '14 at 18:06
  • @JaredFarrish I do have a database with all the artists but I think considering the methods PHP has to offer I'm going the other way around and going to use SQL for this but I'm not sure how yet – Bart Scheffer Jul 27 '14 at 18:08
  • @JaredFarrish since there might be more than one artist I'm not sure how to proceed on searching from the SQL end – Bart Scheffer Jul 27 '14 at 18:09
  • What I would probably do is, when the article is inserted, use the list of artists to find the artists mentioned and save that into a field for the article. Then when someone views the article, use [`MATCH ... AGAINST`](http://dev.mysql.com/doc/refman/5.0/en/fulltext-search.html) to find related articles (and order/display as appropriately). What you seem to be demonstrating, I don't know I see the value. – Jared Farrish Jul 27 '14 at 18:11
  • See [here](http://stackoverflow.com/a/759604/451969) for basic examples of both search methods. – Jared Farrish Jul 27 '14 at 18:13
  • Actually, nevermind, I was thinking of `INTERSECT`, not `COALESCE` (which MySQL also doesn't provide directly). Then you could put the matching artist IDs into a field and do a reverse lookup based on which articles cross both sides of the query/join. See [here](http://stackoverflow.com/a/3201426/451969) for an alternative. – Jared Farrish Jul 27 '14 at 18:21
  • @JaredFarrish MATCH... AGAINST did the trick! Big thanks!
    SELECT * FROM m_artist WHERE match(artist_name) against('+". $_POST['article_content'] ."' IN BOOLEAN MODE) LIMIT 10
    – Bart Scheffer Jul 27 '14 at 18:29

0 Answers0