We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Remove words with numbers from a long string

Pedro Chagas
Pedro Chagas asked
on
Medium Priority
258 Views
Last Modified: 2012-06-21
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

Comment
Watch Question

Pedro ChagasWebmaster

Author

Commented:
Somethimes the string can be too:
$string = "holiday$ in portugal, villa, alga1111rve holidays, algarve villa, villas in portugal, sun, black";
Pedro ChagasWebmaster

Author

Commented:
For help:
The variable $coleta_keywords_final contains "holiday$ in portugal, villa, alga1111rve holidays, algarve villa, villas in portugal, sun, black"
Most Valuable Expert 2011
Author of the Year 2014

Commented:
You can look at each word selectively.  I will post an example that does this.

Best, ~Ray
Pedro ChagasWebmaster

Author

Commented:
Ray, do you have the link for that post?

Regards, JC
Most Valuable Expert 2011
Author of the Year 2014
Commented:

<?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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.