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 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Check next word for numeric value

    I'm searching the $content string for certain words, and if those words are in there, my program flags it. The problem I'm having is that a couple of these words have the option of having a number behind them. I'm not trying to flag the ones with numbers after them.. Is there a way to to this? This is a rough example of what I'm trying to do...


    Code:
    $content = "Crazy Random Apple 43 Left Apple Truck Plane Turtle Duck";
    $needles = array("Random", "Apple", "Duck");
    
    $spaced = split(" ", $content);
    foreach($needles as $pin) {
    foreach($spaced as $cstring) {
    if($cstring == "Apple") { $alert = '1'; } // Found a word with possible number afterwards
    elseif($alert == '1') {
    if(is_numeric($cstring)) { $alert = '0'; } // Not what I want, so I reset the marker
    else { flag($cstring); } // Flagged, and marker reset
    }
    elseif ($cstring == $pin && $alert != '1') { flag($cstring); } // Flag regular word
    }}
    My thought was to check to see if it's one with a possible number, then set an alert. The program would then go to the next word and since the alert was set, it would check to see if the next word is a number or not and respond accordingly, and then of course I'm still checking for the other word(s)..

    "Apple" still gets flagged twice... It should only be flagged once.

    Last edited by m3bik; 02-24-2010 at 02:37 AM.

  • #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
    Really, the best way to do this would be a regular expression. But I like your loops, they are more logical than some bizzare string of symbols.

    So, if you want to stick with the loop concept, I would use an iterator in your loop, so I can simply check the next index using the iterator, rather than carrying some alert flag on to the next iteration in the loop.

    You can stick with "foreach" but a "for" loop has iterator handling built in, so I prefer it for this kind of thing.

    PHP Code:
    foreach ($needle as $pin) {
        for (
    $i 0$i count($spaced); $i++) {
            if (
    $spaced[$i] == $pin) {
                if (
    $i count($spaced) - && !ctype_digit($spaced[$i+1]) {
                    
    flag($spaced[$i]);
                }
            }
        }


  • #3
    New to the CF scene
    Join Date
    Feb 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Fumigator!! I hadn't even considered a for loop instead of the foreach. Very much appreciated.


  •  

    Posting Permissions

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