Question

i am writing a blog script .

i want to find tags from the article ..

what is the best way to find tags from the body string in php

Example : if an article body contains this string

'This Question is about php strings'

so the script should get php string and Question as tag .

Was it helpful?

Solution

There are many ways, but one of them could be to use strpos:

$mystring = 'This Question is about php strings';

$wordsToFind = array('php','strings','Question');

foreach ($wordsToFind as $word)
    if (strpos($mystring, $word)) {
        echo $word.' has been found'; echo "\n";
    }

You can try it out here

OTHER TIPS

You have to define the tags first. Then then a script will find the any one of it is present in the article and add the tags in another array() and that will be the tags for that particular article.

$defined_tags = array('php', 'string', 'questions', 'javascript', 'python');
$article_tags = array();
$string = "This Question is about php strings";

foreach($defined_tags as $tag)
{
    if (strpos($string,$tag) !== false) 
    {
        $article_tags[] = $tag;    
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top