We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Remove words with numbers from a string

Pedro Chagas
Pedro Chagas asked
on
Medium Priority
339 Views
Last Modified: 2012-06-27
Hi, I try to remove some words from a string, and I need some help.
I want remove words whit numbers, like paa034000060, and remove words whose content is not a-z or 0-9, like &exp.
If I have the string:
$string = "the world is12 round and blu&";

After I want a string like:
$string = "the world round and";

Regards, jc
Comment
Watch Question

I suppose you could use:

ereg_replace although it doesn't seem very efficent

Do something like this:
$string = "abcd123#$%";
 
$string = ereg_replace("0", "", $string);
 
$string = ereg_replace("1", "", $string);
 
$string = ereg_replace("2", "", $string);

Open in new window

Or, you could pass the string through javascript which has a much easier way of doing this.  I guess it just depends on what you want to do with the string when your done?
Pedro ChagasWebmaster

Author

Commented:
After this, the string go to data base!
Attention, I want erase or remove the words that no contain a-z or 1-2. Preserve words like glass, painter and 888, erase words like glass11, glas55s, %painter" or paint&er.
Top Expert 2008
Commented:
Try this:
$words = explode(' ',$string);
$result = array();
foreach($words as $word)
  if(!eregi('[^a-z]',$word) or 
     !eregi('[^0-9]',$word)) 
    $result[]=$word;
$string = implode(' ',$result);
echo $string;

Open in new window

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

Ask the Experts
Pedro ChagasWebmaster

Author

Commented:
Perfect
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.