I need to verify the presence of a number in a string. This is the code I had used:
$line='1';
$a=1;
echo strpos($line, $a);
if (strpos($line, 1) !== false) {
echo 'ok';
}
I also tried this:
$line='1';
$a=1;
echo strpos($line, $a);
if (strpos($line, 1+'0') !== false) {
echo 'ok';
}
In either case, however, it doesn't work.
From the documentation:
If needle is not a string, it is converted to an integer and applied as the ordinal value of a character.
Since you're passing 1 as the needle, it's being converted to the character with that code, i.e. Control-A. So it's not looking for the digit 1 in the string. If you want to look for the character 1, you have to use a character string, not an integer:
$a = '1';
echo strpos($line, $a);
if(strpos($line, '1') !== false) {
echo 'ok';
}
If you're initially given a number, you can convert it to a string with strval():
$a = 1;
$a = strval($a);
echo strpos($line, $a);
if(strpos($line, $a) !== false) {
echo 'ok';
}
The strpos works on strings. If you do this
$line='1234';
$a='1';
echo strpos($line, '1');
if(strpos($line, '1') !== false){
echo 'ok';
}
or
$line='1234';
$a='1';
echo strpos($line, $a);
if(strpos($line, $a) !== false){
echo 'ok';
}
It will work
strpos takes three arguments where third argument is optional i.e
strpos(string,find,start). Here find should be of string type if it is not of string type then it is converted to an integer and applied as the ordinal value of a character. Means it takes its ascii value character. For eg.
$string="ABC";
$find=65;
$pos=strpos($string,$find);
echo $pos
Output:
0
i.e position of character 'A' because 65 is an ASCII value of 'A' character.
So when you passed integer value 1 to the strpos function it is converted to some ASCII character and it will return false in your case.
It might be possible that strpos function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. So, use the === operator for testing the return value of this function.
To work your code fine you have to convert the integer type to string type.
maybe you can use the php function ord(), and check if the number is in the range of a ASCII for the numbers.
Related
I have a variable that outputs as 0 type int
I have another string that outputs as 0,1,2.
I am trying to find out if the string contains that very integer, both of these are dynamic values and being fetched from database.
I have tried -
if (strpos($string, $int) !== false) {
echo 'true';
}
But it is not working.
You can use type conversion:
if (strpos($string, (string) $int) !== false)
echo 'true';
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
why does if i do:
if(strpos("[","[rgeger]")){
echo 'hey';
}
it doesn't prints anything?
try this with a string:
function return_tags($keywords){
if($keywords){
$k_array = array();
foreach($this->check($keywords) as $key=>$value){
array_push($k_array, $value);
}
$last_array = array();
foreach($k_array as $key=>$value){
if(strpos("[", $value) && strpos("]", $value) && strlen($value) >= 2){
$value = '<span class="tag">'.$value.'</span>';
}
array_push($last_array, trim($value));
}
return $last_array;
}else{
return false;
}
}
string example
$keywords = "freignferi eiejrngirj erjgnrit [llll] [print me as tag]";
did you see any <span> element printed in html?
It looks like you swapped the arguments:
int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )
So if you want to know if a [ is in your string [rgeger]:
if (strpos("[rgeger]", "[") !== false) {
echo 'hey';
}
(Source: http://php.net/strpos)
The position returned is zero-indexed, so the first character is at position 0. When evaluating 0 as Boolean, it's false - so it doesn't get into the block.
Fix with this:
if (strpos('[rgeger]', '[') !== false)
Also the arguments are the wrong way round. Don't upvote this post any more; go upvote robbi's instead for spotting that one :) Eagle eyes robbi, EAGLE eyes :P
Because you have to check the return value of strpos() as it could be zero because [ it's at index zero, and zero evaluates to FALSE that's why it wouldn't enter your if block, so check that the return value it's FALSE and type boolean, not just integer zero, like this:
if(strpos("[rgeger]","[") !== false){
echo 'hey';
}
UPDATE:
The parameters were in wrong order too, the subject string comes first then the search string, I updated my code above to reflect that.
Because its wrong. Strpos give false if the condition is false.
if(strpos("[rgeger]","[") !== false){
echo 'hey';
}
Edit: I have corrected my answer. Your parameter are in the wrong order. Its:
int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )
Because actually strpos can return 0 see doc.
Warning
This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.
So you have to compare with the falsevalue directly.
if(strpos("[","[rgeger]") !== false){
echo 'hey';
}
EDIT ::
Careful.. Look at the arguments order
int strpos ( string $haystack , mixed $needle [, int $offset = 0 ] )
Haystack is the string input.
Needle is what you are seeking for.
if(strpos("[regeger]","[") !== false){
echo 'hey';
}
I'm trying to do some validation in PHP, and one of them is checking whether there is a specific word in the inputted string or not.
The problem is, my code seem to not working when I put the specified word first.
here's the code:
$word = "aa bb cc dd";
if(strpos($word, 'aa') == false)
{
echo "wrong input";
}
but if I change the $word to either bb aa cc dd or bb cc dd aa, it works. I wonder how to fix this though.
strpos will return false if your string isn't there. Otherwise, it returns the position of your string.
In this case, 'aa' 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, 'aa') === false)
That's because strpos returns the position of the word, in this case 0. 0 is falsey. == does not check for identical matches, === does. So use a triple equals.
It's even in the docs.
strpos is returning 0, as 'aa' is the 0th character. As 0 == false but does NOT === false (it is not boolean), you need to use === instead of ==.
You should use the strict comparison operator, this will match against the same type, so using === will check if it's a Boolean:
if(strpos($word, 'aa') === false)
{
echo "wrong input";
}
Using == is a loose comparison, anything can be stated true (apart from true, 1, string), e.g.
"false" == false // true
"false" === false // false
The reason why it's false because it's comparing a string against a Boolean which returns false.
Because the position of aa is 0, which equals to false.
You have to use:
if(strpos($word, 'aa') === false)
Add a space before search string and find more than 0 position
if(strpos(" ".$word, 'aa') > 0)
{
echo "Found it!";
}