<?php
$myString="abcde";
if($myString=="bc"){echo "yes";}
else{echo "no";}
?>
The code above will echo “no”.
The code below will produce a syntax error but it will show what I want.
<?php
$myString="a[COLOR="Blue"]bc[/COLOR]de";
if($myString [COLOR="Red"]contains[/COLOR] "[COLOR="blue"]bc[/COLOR]"){echo "yes";}
else{echo "no";}
?>
rpkamp
November 6, 2010, 9:26pm
2
Take a look at [fphp]strpos[/fphp]
As ScallioXTX states, maybe [fphp]strpos/fphp can help; here’s a quick example.
<?php
$haystack = 'abcdefghijk';
if(false !== strpos($haystack, 'bc')){
#found
}
?>
You could also create a quick function to make it a little easier.
<?php
function str_contains($haystack, $needle, $case_sensitive = false){
return false !== call_user_func_array(
(bool)$case_sensitive ? 'strpos' : 'stripos' ,
array(
$haystack,
$needle
)
);
}
var_dump(
str_contains('abcdefg', 'bC')
);
/* bool(true) */
var_dump(
str_contains('abcdefg', 'bC', true)
);
/* bool(false) */
?>
system
November 6, 2010, 10:13pm
4
another option
<?php
function isInString($myStr,$testStr,$caseSensitive) {
if($caseSensitive) {
return stripos($myStr,$testStr);
} else {
return strpos($myStr,$testStr);
}
}
$myString ="abcde";
echo (isInString($myString,'bc',false) === false)? 'no' : 'yes';
?>
dotJoon
November 6, 2010, 10:19pm
5
The code below which I wrote applying your code says no because it’s case-sensitive.
I like to make it say “yes”
How can I make it case-insensitive? for making it say"yes?"
<?php
$myString="abcde";
if(false !== strpos($myString, '[COLOR="Red"]B[/COLOR]c')){
echo "yes";}
else{echo "no";}
?>
rpkamp
November 6, 2010, 10:21pm
6
Use stri pos instead of strpos
The i stands for “case insensitive”
dotJoon
November 6, 2010, 10:24pm
7
Thank you for making me to remember it.
Did you see the second block of code?