Remove words with numbers from a string

Posted on 2009-02-16
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
Question by:Pedro Chagas
    LVL 3

    Expert Comment

    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

    LVL 3

    Expert Comment

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

    Author Comment

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

    Accepted Solution

    Try this:
    $words = explode(' ',$string);
    $result = array();
    foreach($words as $word)
      if(!eregi('[^a-z]',$word) or 
    $string = implode(' ',$result);
    echo $string;

    Open in new window

    LVL 3

    Author Closing Comment

    by:Pedro Chagas

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
    Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
    The viewer will learn how to count occurrences of each item in an array.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

    779 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

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now