Jump to content

Check next word for numeric value


AtomicRax

Recommended Posts

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.... :(

Link to comment
Share on other sites

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. :)

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.