Newbie, Does stringA contain StringB? How to??

A

Anonymous

Guest
I am trying to do some simple comparison and can't figure out what I am doing wrong.
Here's the first thing I tried.

$found = eregi("United Parcel Service",$title);
if ( $found === true ) {
$special_shipping="true";
}

$title contains "United Parcel Service (1 x 1.5lbs) (Ground):"

Why isn't $found true?

I also tried
$found = strpos($title,"United Parcel Service")
I couldn't get this to return true either.

I am outputting $title so I know for a fact that it contains the right info.

Thanks for any help
 
im kinda confused

i am seeing

Code:
!== and ===

nowadays.........could someone explain them?


----

edit: cgchris99, try to use
Code:
==
instead of
Code:
===
 
I'm not sure why the regular expression match doesn't work, but in this situation, regular expressions are just overkill. strpos() is quite a lot faster. I'm guessing that the reason that your strpos() test didn't work is because you forgot to swap the arguments around, e.g. strpos($haystack, $needle). For me, the following code worked just fine:

Code:
<?
   $title = "United Parcel Service (1 x 1.5lbs) (Ground):";

   $found = strpos($title, "United Parcel Service");

   if($found !== false) {
      echo 'substring found';
   } else {
      echo 'substring not found';
   }
?>

Virtuoso, the short answer to your question is that == means equal to, and === means identical to. Likewise != means not equal to and !== means not identical to. For more discussion on this, refer to this thread, and this page and this page in the documentation.

The reason it's correct for cgchris99 to use === or !== in this context is because if the needle happens to be at the very beginning of the haystack, strpos() will return 0 as the index, but PHP considers 0 to be equal to false, which would foul things up. Instead, he uses the identical operator so he can distinguish between 0 and false, which PHP knows are not identical.

That's a poor explanation, but I hope it suffices. See the links above for examples.
 
$found = eregi("United Parcel Service",$title);

isn't it suppost to be ereg and not eregi?

this is how i been doing my stuff like this and it works for me..
Code:
if(ereg("United Parcel Service",$title))
{ 
$special_shipping="true"; 
}
 
eregi() is the non-case-sensitive equivalent of ereg(). But if you're just doing simple matching, just use strpos() and not ereg(). It's much faster.

Some days I get the feeling that there are PHP books being published out there that are continually telling beginning programmers to do the wrong thing.
 
eregi() is the non-case-sensitive equivalent
cool, might come in handy

Code:
published out there that are continually telling beginning programmers to do the wrong thing
I learned it by lookin at other scripts. Ya, misprints and such are not good. but even if one way is more efficient, it's good to know a few diferent ways, eh?
 
Back
Top