473,320 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Issues with strpos

Not quite sure what's happening here, the docs for strpos() say:

"Returns the position as an integer. If needle is not found, strpos()
will return boolean FALSE."

Looks fine, I can simply do:

if(strpos("michael", "m")

But then the docs go on to say:

"This function may return Boolean FALSE, but may also return a non-
Boolean value which evaluates to FALSE, such as 0 or "". Please read
the section on Booleans for more information. Use the === operator for
testing the return value of this function."

This seems a little inconsistent to me, means I can't do the
following:

if(strpos("michael", "z") >= 0)
{
//found it!
}

Because that always returns as found, which clearly it isn't. I can
use comparison operators to check for type, but only using "===" or "!
==".

I only want a function to tell me if the string was found (>= 0), I
don't care which position it was etc

What do others use to do this? Preferably without converting to an
array then doing an array find or something.
Jun 2 '08 #1
1 1247
On May 31, 10:58 am, ljb <ljb1...@pobox.comwrote:
sha...@gmail.com wrote:
Not quite sure what's happening here, the docs for strpos() say:
"Returns the position as an integer. If needle is not found, strpos()
will return boolean FALSE."
Looks fine, I can simply do:
if(strpos("michael", "m")
But then the docs go on to say:
"This function may return Boolean FALSE, but may also return a non-
Boolean value which evaluates to FALSE, such as 0 or "". Please read
the section on Booleans for more information. Use the === operator for
testing the return value of this function."
...
I only want a function to tell me if the string was found (>= 0), I
don't care which position it was etc

Use: if (strpos("michael", "m") !== False) ...
Thanks ljb
Jun 2 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Jonathan Lamothe | last post by:
I'm helping someone to create an online database. All is fine and good except for one problem. Here it is: In order to provide connectivity to the database, I've created a file called...
2
by: yawnmoth | last post by:
say i have the following php script: <? if (!is_int(strpos("a","aaa"))) // if (strpos("a","aaa") === false) print "no match"; else print "match"; ?> when i run it, i am told there is no...
2
by: ralphNOSPAM | last post by:
I'm trying to use strpos to find these xml tags so I can pull out the text data; but the bracket chars won't let the function work. Is there another way to do this? $php_XML_tag =...
3
by: julian_m | last post by:
file AddHTML.htm: ---------------------------- This line is <b>ok</b><br> This line is <b>ok</b><br> This line is <b>ok</b><br> <?PHP echo "This one has code prohibited "; ?> This line is...
2
by: drec | last post by:
I am trying to right an if statment using strpos to determine if the string exists in the variable, however I seem to be getting the wrong effect. Here is my script. <?php $dn = "ABC-DEF"; ...
1
by: Dana Cartwright | last post by:
At php.net, the following paragraph is the first thing in the definition of strpos: "Returns the numeric position of the first occurrence of needle in the haystack string. Unlike the strrpos()...
18
by: jodleren | last post by:
Hi! I was reading php.net for a way to find a number of characters in a strings. Say I want to look for a b and c ind $string. As far as I could see, there is no function for that. So, I can...
2
by: daschicken | last post by:
Hi, ive written the following php code. its a part of an admin form to add a text. when adding the text %bild the script is supposed to swap that with the link to the picture already stored in the...
3
by: Tarik Monem | last post by:
Hi, here I am again :-) I'm trying to compare the contents of two arrays and if there's a match, then I would like to place the contents of that one index of the array into a "new" third array. ...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.