Remove words with numbers from a long string

Hi, I have a string and inside this string I collect some phrases like:
$string = "holiday$ in portugal, algarve villa, alga1111rve holidays, algarve villa, villas in portugal,";
I want to remove the words like holiday$ and alga1111rve, and after I remove that words I get the string again preserving the commas(,), like:
$string = "in portugal, algarve villa, holidays, algarve villa, villas in portugal,";

Inside snipped code, you can see my script, but in final $coleta_keywords_final is empty!!!!!
Attention:  $coleta_keywords_final, the string have allways data formated like "in portugal, algarve villa, holidays, algarve villa, villas in portugal,".

What I do wrong?

Regards, JC
$keep = explode(',', $coleta_keywords_final); 
$coleta_keywords_final = implode(" , ", $keep);
$words = explode(',',$coleta_keywords_final);
$result = array();
foreach($words as $word)
if(!eregi('[^a-z]',$word) or 
!eregi('[^0-9]',$word)) 
$result[]=$word;
$coleta_keywords_final = implode(',',$result);
echo "<br> $coleta_keywords_final <br>";

Open in new window

LVL 3
Pedro ChagasWebmasterAsked:
Who is Participating?
 
Ray PaseurCommented:

<?php // RAY_temp_editwords.php
 
// SOME TEST DATA
$raw_data = "holiday$ in portugal, villa, alga1111rve holidays, algarve villa, villas in portugal, sun, black";
 
// MAKE SURE THERE ARE NOT REPLICATED BLANKS
$my_data = trim(ereg_replace(' +', ' ', $raw_data));
 
// BREAK INTO ARRAY ON BLANKS
$array = explode(' ', $my_data);
 
// CREATE AN OUTPUT STRING
$out_data = '';
 
// ITERATE OVER THE ARRAY OF WORDS
foreach ($array as $word)
{
 
// APPLY ANY TESTS YOU WANT TO THE WORDS HERE
   if (ereg('\$', $word))    continue; // SKIP WORD WITH DOLLAR SIGNS
   if (ereg('[0-9]', $word)) continue; // SKIP WORDS WITH NUMBERS
 
// PUT ACCEPTABLE WORDS INTO THE OUTPUT STRING
   $out_data .= $word . ' ';
}
 
// TRIM OFF THE TRAILING NOISE
$out_data = rtrim($out_data, ' ');
 
var_dump($out_data);

Open in new window

0
 
Pedro ChagasWebmasterAuthor Commented:
Somethimes the string can be too:
$string = "holiday$ in portugal, villa, alga1111rve holidays, algarve villa, villas in portugal, sun, black";
0
 
Pedro ChagasWebmasterAuthor Commented:
For help:
The variable $coleta_keywords_final contains "holiday$ in portugal, villa, alga1111rve holidays, algarve villa, villas in portugal, sun, black"
0
 
Ray PaseurCommented:
You can look at each word selectively.  I will post an example that does this.

Best, ~Ray
0
 
Pedro ChagasWebmasterAuthor Commented:
Ray, do you have the link for that post?

Regards, JC
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.