Jump to content

Recommended Posts

Well i was given a task to rearrange/format words too related keywords, a visitor would submit a list of keywords and can also submit keywords to ignore (optionally - in another textarea), then it would sort the keywords by two word phrase; top recurring keywords which would give the output.

 

When i was assigned the task this example was given:

 

INPUT KEYWORDS (Via $_POST ->new line per word):

real property Miami
Miami real property for sale
Miami homes for sale
homes in Miami
new homes in Miami
Miami houses for sale
Miami houses
houses in Miami
real property Los Vegas
Los Vegas real property for sale
Los Vegas homes for sale
homes in Los Vegas
new homes in Los Vegas
Los Vegas houses for sale
Los Vegas houses
houses in Los Vegas

 

 

IGNORED KEYWORDS (Via $_POST ->new line per word):

Miami
Los Vegas

 

 

OUTPUT (comma separated && new line per word):

real property,real property Miami
real property,real property Los Vegas
real property,Miami real property for sale
real property,Bevery Hills real property for sale
homes,homes in Miami
homes,homes in Los Vegas
homes,new homes in Miami
homes,new homes in Los Vegas
houses,Miami houses for sale
houses,Miami houses
houses,Los Vegas houses for sale
houses,Los Vegas houses
houses,houses in Miami
houses,houses in Los Vegas

 

The trouble, is i need your help to think of how I could achieve, this ie. the methodology behind this?. I've already started it but im getting to the point where im lost on how to get the output (like the example above).

 

 

My code:

 

<?php
if(isset($_POST['submit'])){

if(!empty($_POST['keywords'])){

$words = str_replace(array("\r\n", "\r"), "\n", $_POST['keywords']);

//words array
$wordsArray = explode("\n", $words);

$words2 = str_replace(array("\r\n", "\r"), "\n", $_POST['ignorewords']);

//ignore words array
$wordsArray2 = explode("\n", $words2);

//remove found ignore'd words for words array
foreach(array_intersect($wordsArray, $wordsArray2) as $match)
{
    foreach(array_keys($wordsArray, $match) as $key)
    {
        unset($wordsArray[$key]);
    }
}


$comma_separated = implode(",", $wordsArray);

$words = array_count_values(str_word_count($comma_separated, 1));
arsort($words);
$words = array_slice($words, 0, 2);

//this gives the top 2 recurring words.
print_r($words);

}

}

?>

 

 

All help is appreciated.  :-\

 

Link to comment
https://forums.phpfreaks.com/topic/190249-how-would-this-work/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.