• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

Help Building a Function to create coma separated string

Hi,

I need help creating a PHP function CreateMetaKeywords($string);

I have one string with a description:

$description = "Create Wind And Solar' Power Systems are alternative & ecologic systems* ";

$exclude array = "create, last, plane";

function should return a string:

1- excluding all non alpha characters
2- excluding the words in $exclude array.
3- excluding words with <= 3 characters
4- Return first 6 remaining words separated by a comma.

Output should be:

"Wind, Solar, Power, systems, alternative, ecologic"

I do appreciate any help with this code.

Thank in advance.
0
Fernanditos
Asked:
Fernanditos
  • 2
1 Solution
 
Ray PaseurCommented:
<?php // RAY_temp_fernanditos.php
error_reporting(E_ALL);

$description = "Create Wind And Solar' Power Systems are alternative & ecologic systems* ";

$exclude = array
( "create"
, "last"
, "plane"
)
;

var_dump( createmetakeywords($description, $exclude) );

function CreateMetaKeywords($string, $exclude=array())
{
    // 1- excluding all non alpha characters
    // 2- excluding the words in $exclude array.
    // 3- excluding words with <= 3 characters
    // 4- Return first 6 remaining words separated by a comma.

    // NORMALIZE TO UPPER CASE
    foreach ($exclude as $key => $word)
    {
        $exclude[$key] = trim(strtoupper($word));
    }

    // RULE 1
    $string = preg_replace('#[^A-Z ]#i', ' ', $string);
    $string = preg_replace('#\s\s+#',    ' ', $string);
    $string = trim($string);

    // RULE 2 AND 3
    $array = explode(' ', $string);
    foreach ($array as $key => $word)
    {
        if (strlen($word) <= 3)
        {
            unset($array[$key]);
        }
        if (in_array(strtoupper($word), $exclude))
        {
            unset($array[$key]);
        }
    }

    // RULE 4
    $array = array_slice(array_values($array),0,6);

    // RETURN THE WORD
    return implode(',', $array);
}

Open in new window

0
 
Amar BardoliwalaCommented:
Hello  Fernanditos

Following is code which can help you in creating a function you want

<?php
$mystr =  "Create Wind And Solar' Power Systems are alternative & ecologic systems* ";

$exclude_array = array("create", "last", "plane");

$mystr = preg_replace("/[^A-Za-z ]/", "", $mystr);

$mystr = str_ireplace($exclude_array,"",$mystr);

$mystr = preg_replace("/\b[^\s]{1,3}\b/", "", $mystr);

$mystr = preg_replace('!\s+!', ' ', $mystr);

$my_array = explode(" ", trim($mystr));

print implode(",",array_slice($my_array, 0, 6));
?>

Open in new window


Hope this will help you.

Thank you.

Amar Bardoliwala
0
 
FernanditosAuthor Commented:
Amazing, as usual!
0
 
FernanditosAuthor Commented:
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now