Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Remove words with numbers from a string

Posted on 2009-02-16
5
Medium Priority
?
329 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
0
Comment
Question by:Pedro Chagas
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:DavidSingleton
ID: 23651100
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

0
 
LVL 3

Expert Comment

by:DavidSingleton
ID: 23651102
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?
0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 23651160
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.
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 2000 total points
ID: 23651192
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

0
 
LVL 3

Author Closing Comment

by:Pedro Chagas
ID: 31547375
Perfect
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month20 days, 20 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question