EDIT:
Ok for everyone who seems to be mentally disabled or something, check this out how it should be and why Halcyon's solution to swap haystack and needle DO MAKE SENSE:
$test = 'bla';
if(strpos($test, 'hauptkat') !== false OR strpos($test, 'kat') !== false)
{
echo 'hauptkat or kat is inside $test';
}
else
{
echo 'hauptkat or kat is NOT inside $test';
}
I think you've swapped your needle and haystack.
strpos("hauptkat", "kat") // 5
See: strpos.
Related
Right now I use stristr($q, $string) but if
$string = "one monkey can jump 66 times";
$q = "monkey 66";
I want to find out if this string contains both monkey and 66.
How can i do that?
you could use both stristr and strpos.
as it is reported in this post, the second method is faster and less memory intensive.
well, check this lines out:
// here there are your string and your keywords
$string = "one monkey can jump 66 times";
$q = "monkey 66";
// initializate an array from keywords in $q
$q = explode(" ", $q);
// for every keyword you entered
foreach($q as $value) {
// if strpos finds the value on the string and return true
if (strpos($string, $value))
// add the found value to a new array
$found[] = $value;
}
// if all the values are found and therefore added to the array,
// the new array should match the same object of the values array
if ($found === $q) {
// let's go through your path, super-man!
echo "ok, all q values are in string var, you can continue...";
}
if(stristr('monkey', $string) && stristr('66', $string)) {
//Do stuff
}
simply post your variable value by giving them a variable $monkey,$value ($monkey jumps $value) and then fetch its value
You can use the strpos() function which is used to find the occurrence of one string inside another one:
$a = 'How are you?';
if (strpos($a, 'are') !== false) {
echo 'true';
}
Note that the use of !== false is deliberate (neither != false nor === true will work); strpos() returns either the offset at which the needle string begins in the haystack string, or the boolean false if the needle isn't found. Since 0 is a valid offset and 0 is "falsey", we can't use simpler constructs like !strpos($a, 'are').
I know the $a variable with the tag is not properly formatted, however that's irrelevant to the issue.
The issue is that strpos is looking for a forward slash, /, in the value of each key in the array, but it is not printing.
$a = '<a target="" href="/test/url">test';
$a_expanded = explode("\"", $a);
echo print_r($a_expanded);
foreach($a_expanded as $num => $aspect) {
echo $aspect;
if ($contains_path = strpos($aspect, '/')) {
echo $a_expanded[$num];
}
}
It echos the array and each aspect, but will not echo the string with the forward slashes when found by strpos.
if ($contains_path = strpos($aspect, '/'))
should be
$contains_path = strpos($aspect, '/');
if ($contains_path !== false)
as strpos will return 0 when the string directly starts with a / (as it does, in your case). If strpos has no match, it returns false.
if (0) and if (false) are the same. So you need to do strict comparison (=== or !==) here.
The position of found string might be 0 which is counted as false, you need to compare as ===
if (false !== $contains_path = strpos($aspect, '/')) {
echo $a_expanded[$num];
}
strpos() could either return FALSE, 0, or a non-zero value.
If the needle occurs at the beginning of the haystack, strpos() returns 0.
If it occurs elsewhere, it returns the respective position of the needle in the haystack.
If needle wasn't found in the haystack, strpos() returns boolean FALSE.
I wasn't checking for strict equality, so the if statement always returned a falsey value, causing my code to not work.
if ($contains_path = strpos($aspect, '/'))
To fix the issue, you could use !==, which compares the type and value:
if ($contains_path = (strpos($aspect, '/') !== FALSE))
For more information, check the following links:
http://php.net/strpos
http://www.php.net/manual/en/language.operators.comparison.php
In the following code, if I set $what to 'red', it doesn't find it, whereas it finds green and blue. Why and how to make it find red as well?
$where = 'red,green,blue';
$what = 'blue';
if (strpos($where, $what) == true) {
echo 'found';
}
strpos returns the index of the found string. In this case the index is 0 and your check for == true will fail. Try:
strpos($where, $what) !== false
The documentation provides more information.
strpos will return false if your string isn't there. Otherwise, it returns the position of your string.
In this case, 'red' is at the start of the string, which means that it's at position 0; and 0 evaluates to false.
You need to do a boolean compare on the result:
if(strpos($word, 'red') === false)
In PHP, how to compare two "alike" data? For instance, in this code:
$a = "cats are cool";
$b = "1. catS are cool!!!"
if($a == $b) {
echo "TRUE";
}
else {
echo "FALSE";
}
now the obvious output will be "FALSE". But to what I'm trying to achieve, as long as the keyword "cats are cool" is in $b, the result should be "TRUE". How do I do this?
If you are looking for an exact match, use stripos().
//Note the use of !== here, it's because stripos may return 0,
//Which would be interpreted as false without strict comparison.
if (stripos($string, "cats are cool") !== false) {
//Cats are cool indeed.
}
You have to define your own logic, period. Either you look for keywords [e.g. you search how many occurrences of your whitelist pop up in your strings], or you compute some string distance metric, like Levenshtein distance.
If you should find just first string in second you can use strpos
use stripos()
if(stripos($b,$a) !== FALSE)
echo "found";
else
echo "not found";
I have variable x and I want to see if it contains a string like hs even though the value may be cug hs ib ap. Can anybody help me figure this out? Thanks!
PHP strstr
<?php
$x = 'cug hs ib';
$y = strstr($x, 'hs');
echo $y;
?>
Update:
Better user strpos
<?php
$x = 'cug hs ib';
$y = strpos($x, 'hs');
if($y === false)
// Not a substring
else
// Substring
?>
if(strpos($big_string, $sub_string) !== false)
{
// $big_string contains $sub_string
}
You could use strpos
if (strpos($haystack, $needle) !== false) {
echo "the string '{$needle}' was found within '{$haystack}'";
}
You could use substr_count .. http://php.net/substr_count or strpos, http://php.net/strpos
Either strpos() or stripos() (depending on whether you're interested in case sensitivity).
http://php.net/manual/en/function.strpos.php