Ask a Question related to PHP Development, Design and Development.
-
Dvdmandt #1
Re: php equivalent to asp's instr()
Hmm... Are you sure strpos doesn't work for you? Sure worked for me when I
wanted something like that Otherwise, checkout following:
[url]http://se2.php.net/strings[/url]
function instr($needle, $haystack,$case_sensitive=true)
{
return
(($case_sensitive?strpos($needle,$haystack):stripo s($needle,$haystack))!==fa
lse);
}
try that, although, that's just from the top of my head, not tested or
anything and it's my pretty bad coding style...
also, take a look at ereg()...
--
// DvDmanDT
MSN: [email]dvdmandt@hotmail.com[/email]
Mail: [email]dvdmandt@telia.com[/email]
"Chris W. Parker" <cparker@swatgear.com> skrev i meddelandet
news:001BD19C96E6E64E8750D72C2EA0ECEE1AE1D8@ati-ex-01.ati.local...
Hi.
I don't know why but I've had the darndest time trying to find an easy
equivalent of asp's InStr() function in PHP. I know there is
preg_match(), strpos(), and strstr(), but none of them work like I want
them to.
In fact, except for maybe preg_match(), I can't get any of them to work
right.
I want a function that will return true if the string is found, and
false if it is not found.
Like this:*
$needle = "&";
$haystack = "Belle & Sebastian";
if(FoundInString($needle, $haystack))
{
echo "found!";
}
else
{
echo "not found!";
}
Anyone have a function to share?
Thanks,
Chris.
* Anyone catch the Belle & Sebastian show at the Greek Theatre in LA
this past sunday?
Dvdmandt Guest
-
advice on how to handle INSTR search?
Gentlemen; I have a situation that should be quite minor but it seems I have struggling with it for some reason or another. Here's what's up: ... -
sql greatest,decode,instr. minimize code lines sql
I did not write this code and please excuse me for my beginner questions? ? Is there a way to make this code cleaner. Code it where its less... -
INSTR function and case sensitivity
I am using the INSTR function to do a match on a text field that collects form data with the same field in my database. It isn't finding a match if... -
InStr, substr, Mid ???
I have a field name "tracks" of string data type that has multiple names in it (for example: calder delaware$ aqueduct ), and I want to check to... -
how to print instr ptr from stack in signal handler?
Hi, Want to catch SIGSEGV in a signal handler, print some meaningful info, then let things proceed to core sump. Know how to do all this and have... -
David Cuddeback #2
Re: php equivalent to asp's instr()
Hi Chris,
I've always used strstr() for this _exact_ same purpose and it's worked just
fine. I find it much more useful as an InStr()-like function than for
actually doing anything with the string it returns. You'll want to note
that the parameter order is $haystack before $needle. That goes for
strpos() as well. I noticed that in your original post, you're using a
hypothetical function FoundInString($needle,$haystack). If you used the
same order ($needle,$haystack) when you tried strstr() or strpos(), then it
would fail because it's a bit difficult to find a haystack in a needle. =/
I _strongly_ recommend using one of these built-in functions, as they will
run much faster than any programmer-defined function. Below I will explain
how and potential pitfalls.
Only problem I noticed is that you want the function to return an explicit
true if the string is found (and false if it is not). Both functions will
return an explicit false if the needle is not found, but neither return an
explicit true. Don't let that stop you from using them though. If needle
is found, strstr() returns a string and strpos() returns an integer
(possibly 0). An if statement will consider a string (as returned by
strstr()) as logically true, so you can use strstr() by itself inside an if
statement. strpos() however returns the offset from the beginning of
$haystack where it found $needle. This means that if $needle is at the
beginning of $haystack, then it will return an integer 0. This of course is
considered false by an if statement. However, you can use
if(strpos($haystack,$needle)!==false). If you take a look at
[url]http://us3.php.net/manual/en/language.operators.comparison.php[/url], you'll see
that PHP's $a===$b and $a!==$b operators result in a true only if $a and $b
are of the same type (bool, int, etc). Since an integer 0 is not the same
as a boolean false, strpos()!==false will not choke when it returns an
integer 0.
If you want the function to return an explicit true because you want to
assign the result to a variable, you can do this:
$foo = (strstr($haystack,$needle)!==false)
or anywhere else you plan to use it for that matter.
Documentation for strstr() and strpos()
[url]http://us3.php.net/strstr[/url]
[url]http://us3.php.net/strpos[/url]
David Cuddeback Guest
-
Unregistered #3
Re: php equivalent to asp's instr()
function inStr($searchIn,$stringToSearch){
$x=-1;
$x=strpos(strtoupper($searchIn),strtoupper($string ToSearch));
if ($x>-1) return TRUE; else return FALSE;
}
* Anyone catch the Belle & Sebastian show at the Greek Theatre in LA
this past sunday?[/QUOTE]Unregistered Guest



Reply With Quote

