Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts

    Eliminating an element from an array ?

    Hi,

    In my array of words, I sometimes get some gibberish and sometimes get
    "words" that are just a space.

    To eliminate them I have chosen a rather arbitrary word length of 25 characters. Anything over that is probably not a real word.

    My words are in a string,
    so I get my word list into an array with this:

    $words = explode(" ", $text);

    Before doing that I would like to get rid of
    those long words and the single-space-only words

    I guess that I need a regex
    (or two of them )
    ?
    PHP Code:
    $regex " ??? ";
    $text preg_replace($regex,"",$text); 
    Any ideas what the regex(s) should be ?

    Thanks

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    I never use code I don't understand. I would recommend the same to you. If someone else writes regex that you just plug in with no idea how it's working, it will be impossible for you to maintain that code.

    So, there are plenty of ways to do this without any regex. In my opinion, the simplest way is the best way. I would loop through the array, check each element, unset it if it doesn't pass your validation rules.

    PHP Code:
    foreach($myArray as $key => $val) {
        if (
    strlen($val) > 25) {
            unset(
    $myArray[$key]);
        }

    Last edited by Fumigator; 11-05-2009 at 04:41 PM. Reason: forgot which language this was for a second

  • #3
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    I totally agree with you,
    regex is something that I am rather weak on and
    I am trying to get my head around.

    Most of my posts recently have been about regex so
    hopefully I will pick it up.

    On good thing about doing that loop is that I could check
    for other things at the same time.

    Presumably I can get rid of those one-space words
    at the same time with:

    PHP Code:
    foreach($myArray as $key => $val) {
        if (
    strlen($val) > 25  || $val == " "  || $val == "" ) {
            unset(
    $myArray[$key]);
        }


  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    You could use an array_filter to capture just what you're looking for:
    PHP Code:
    function myArrayFilter($var)
    {
        
    $var trim($var);
        return !empty(
    $var) && strlen($var) < 25;
    }

    $newArray array_filter($myArray'myArrayFilter'); 
    Try that, does that work?

    Looks like the 25 character max is a pretty good assumption. Check this wiki link (don't know how valid it is...), but it shows aside from 7 pretty much unheard of words that cover the 27+ characters.

    Edit:
    Guess you'll want that link lol: http://en.wikipedia.org/wiki/Longest_word_in_English
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •