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').
Related
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
I am trying to exclude those tweets which are not having RT # in their text.
Here is my code:
foreach ($tweets3 as $item)
{
$text = $item->text;
$check = 'RT #';
$result = strpos($text, $check);
if($result == false)
continue;
}
But these tweets also get excluded
Mention text : RT #IBMcloud: A few #cloud highlights from the #IBM Annual Report. Read more: http://t.co/TJBHoX3vdU http://t.co/fG66SE7kV1
RT #holgermu: MyPOV - Nice way to put out our annual report in an interactive (engaging?) format - here is #IBM's - http://t.co/TIqi0soc5W
RT #TopixPolitix: Chinese State and Citizens Must Battle Airpocalypse Together http://t.co/nV5TGJG6Fl - http://t.co/cln83ufDnk
Though they have RT # in their text. Why?
See this warning in the documentation for strpos():
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Use the === operator for testing the return value of this function.
As the documentation says, strpos() can return values that evaluate to boolean FALSE. For example, strpos() will return 0 if there is a match at the beginning of the string.
To avoid ambiguity, always use strict comparison (===) instead of loose comparison (==) (whenever possible):
foreach ($tweets3 as $item)
{
$text = $item->text;
$check = 'RT #';
$result = strpos($text, $check);
// if "RT #" text not found in tweet, skip to next iteration
if ($result === false) continue;
}
I think your logic is flipped. $result will hold the numerical value if the text is found. You want your check to be:
if($result !== false)
continue;
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)
I am trying to replace the value Event Id in the $fields array with the value that is mapped to (idEvent) in the $aliases array, but PHP's array_search function is returning the wrong position. Note: I am converting the values to all lower case so it should return a match, and it seems like array_search is returning a index, but it should be returning index 2 instead of index 1 since it is the third value in the $fields array.
Unfortunately, if you run the code (e.g. copy and paste it here: http://writecodeonline.com/php/), it returns the wrong value. Could someone please tell me if I am doing something wrong?
$fields = array('Host', 'OS', 'Event Id');
$aliases = array('idEvent' => 'Event ID');
foreach ($aliases as $actual => $alias){
$alias = strtolower($alias);
echo "searching fields(" . implode(',', array_map('strtolower', $fields)) . ") for $alias<br/>";
if ($position = array_search($alias, array_map('strtolower', $fields)) !== FALSE) {
echo "$alias was found at \$fields[$position]";
$fields[$position] = $actual;
}
}
Edit: I added some echo statements so you can what I am trying to do.
It's the order of operations of the if statement that's the problem. The assignment operator has lower precedence than the comparison operator, and the assignment is evaluated from the right first. So add some parenthesis:
if (($position = array_search($alias, array_map('strtolower', $fields))) !== FALSE) {
I find this easier to read:
if (in_array($alias, array_map('strtolower', $fields))) {
array_search($alias, array_map('strtolower', $fields)) !== FALSE
This is true right? Then it translates to $position = 1 and therefor you see it he found the key one but actually returns the value of the equal...
Use parenthesis or store the value of the search beforehand.
$position is getting assigned value of 1 because its the result of comparing your array_search to !== FALSE
use the test at the left side to fix the issue.
if (false !== ($position = array_search($alias, array_map('strtolower', $fields)))) {
echo "$alias was found at {$fields[$position]}";
$fields[$position] = $actual;
}
I have a variable:
$testingAllDay = $event->when[0]->startTime;
This variable will be this format if it is "All Day":
2011-06-30
It will be this format if it is not "All Day":
2011-07-08T12:00:00.000-05:00
I'm wanting to do something like:
if ($testingAllDay does not contain "T"){
$AllDay = 1;
} else {
$AllDay = 0;
}
Do I need to use a strstr() here, or is there another function that does this? Thanks!
One option is to use strpos to see if the 'T' character is present in the string as follows:
if (strpos($testingAllDay, 'T') !== false) {
// 'T' was present in $testingAllDay
}
That said, it would probably be faster/more efficient (although no doubt meaninglessly so) to use strlen in this case, as according to your example, the time-free field will always be 10 characters long.
For example:
if(strlen($testingAllDay) > 10) {
// 'T' was present in $testingAllDay
}
Use strpos:
if (strpos($testingAllDay,"T")!==false){
or strstr
if (!strstr($testingAllDay,"T")){
if (strpos($testingAllDay, 'T') !== FALSE){
...
}
If those are the only possible cases, even strlen() will do.
not exactly answer to the question, but you could check with strlen().
i.e. "All Day" length is 10, anything above that is not.
The function you're looking for is strpos(). The following is an example picking up your wording for the variable names even:
$testingAllDayTPosition = strpos($testingAllDay, 'T');
$testingAllDayDoesNotContainT = false === $testingAllDayTPosition;
if ($testingAllDayDoesNotContainT){
$AllDay = 1;
} else {
$AllDay = 0;
}
strstr and strpos are two functions by which you can complete your requirement.
strstr will see if substring exists in string and it will echo from first occurrence of string to rest.
While strpos will give you position of first occurrence of the string.