Link to home
Start Free TrialLog in
Avatar of Fernanditos
Fernanditos

asked on

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.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
Avatar of Fernanditos
Fernanditos

ASKER

Amazing, as usual!