AtomicRax Posted February 23, 2010 Share Posted February 23, 2010 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... $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 == $needle && $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. No luck at all.... Quote Link to comment https://forums.phpfreaks.com/topic/193039-check-next-word-for-numeric-value/ Share on other sites More sharing options...
salathe Posted February 23, 2010 Share Posted February 23, 2010 We need to know precisely what you're doing at the moment to flag words as any help given could be incompatible with the way you're doing things. So, could you give an overview (code would be awesome) of the process you go through when searching the content? Edit: Thanks... I'll take a look and reply later if no-one else has. Quote Link to comment https://forums.phpfreaks.com/topic/193039-check-next-word-for-numeric-value/#findComment-1016655 Share on other sites More sharing options...
AtomicRax Posted February 23, 2010 Author Share Posted February 23, 2010 elseif ($cstring == $needle && $alert != '1') { flag($cstring); } // Flag regular word Is actually elseif ($cstring == $pin && $alert != '1') { flag($cstring); } // Flag regular word That was just a typo in the post...not me fixing the problem. (Couldn't modify the post anymore) Quote Link to comment https://forums.phpfreaks.com/topic/193039-check-next-word-for-numeric-value/#findComment-1016659 Share on other sites More sharing options...
Wolphie Posted February 23, 2010 Share Posted February 23, 2010 Personally, I'd recommend taking a look at how regular expressions work and how they could help you accomplish what you're trying to do. I've looked at your code, and this works for me although I'm not entirely sure if it's exactly what you're after. <?php $content = "Crazy Random Apple 43 Left Apple Truck Plane Turtle Duck"; $needles = array("Random", "Apple", "Duck"); $words = split(" ", $content); foreach($words as $word) { foreach ($needles as $needle) { if ($word == $needle) { // Flag regular word flag($word); $alert = 1; } } if ($alert == 1) { if (is_numeric($word)) { $alert = 0; } else { flag($word); } } } ?> [code] Quote Link to comment https://forums.phpfreaks.com/topic/193039-check-next-word-for-numeric-value/#findComment-1016661 Share on other sites More sharing options...
AtomicRax Posted February 23, 2010 Author Share Posted February 23, 2010 Personally, I'd recommend taking a look at how regular expressions work and how they could help you accomplish what you're trying to do. I've looked at your code, and this works for me although I'm not entirely sure if it's exactly what you're after. <?php $content = "Crazy Random Apple 43 Left Apple Truck Plane Turtle Duck"; $needles = array("Random", "Apple", "Duck"); $words = split(" ", $content); foreach($words as $word) { foreach ($needles as $needle) { if ($word == $needle) { // Flag regular word flag($word); $alert = 1; } } if ($alert == 1) { if (is_numeric($word)) { $alert = 0; } else { flag($word); } } } ?> No, that doesn't help. Thanks though. Quote Link to comment https://forums.phpfreaks.com/topic/193039-check-next-word-for-numeric-value/#findComment-1016668 Share on other sites More sharing options...
AtomicRax Posted February 24, 2010 Author Share Posted February 24, 2010 For the record: http://codingforums.com/showthread.php?p=925902#post925974 Got it fixed over there. Quote Link to comment https://forums.phpfreaks.com/topic/193039-check-next-word-for-numeric-value/#findComment-1017162 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.