Here's what I'm running:
echo $checknetworks;
Here's the results of the echo:
Facebook,Twitter,Myspace,Google,Instagram,Pinterest
What I want to do is check to see if the string google is in the results above. I do not want it to be case sensitive because the capitalization changes from time to time.
Basically if google exists in the string, I want to display "FOUND". If it doesn't exist, I want to display "NOT FOUND".
I came across a couple of somewhat similar questions here but none seemed to take capitalization into account.
You need stripos:
stripos — Find the position of the first occurrence of a case-insensitive substring in a string
$checknetworks = "Facebook,Twitter,Myspace,Google,Instagram,Pinterest";
if (stripos($checknetworks, 'Google') === FALSE)
{
echo 'NOT FOUND';
} else
{
echo 'FOUND';
}
Please note that you should compare types as well. I.e. if your string would start with google, stripos will return 0, that would be interpreted as false, unless you make the type comparison with ===
try using strpos:
<?php
$strVar = (string)$myVar;
if (strpos($strVar, "Google")){
echo "Found"
}else{
echo "Not found"
}
?>
EDIT:
You must check if the strpos returns FALSE, and not the position 0.
Use '===':
if (strpos($strVar, "Google") === FALSE){
Related
I get a project and i see a piece of code as follows:
<?php
$orderby=$_REQUEST['orderby'];
if(strpos($orderby,'d')===true){
echo "exists";
}else{
echo "not ";
}?>
In any case , i input 'd' or others parameters the page always returning 'not'.
so, how to input correct parameter make the page returning 'exists'?
strpos() can never return TRUE. If the string is found it returns the position. If the string is not found it returns FALSE. So you should compare with FALSE, not TRUE.
if (strpos($orderby, 'd') === false) {
echo "not exists";
} else {
echo "exists";
}
Your test is not saying that it is returning false, just that strpos() never returns a value of a boolean true. Instead it will return an integer with the position of the string found. Normally the check would be
if(strpos($orderby,'d') !== false){
echo "exists";
}else{
echo "not ";
}
if strpos finds a match, it won't give back true but the offset - so your strpos($orderby,'d')===true is never hit.
Try this:
<?php
$orderby=$_REQUEST['orderby'];
if($o=strpos($orderby,'d')===false){
echo "not ";
}else{
echo "exists at offset $o";
}?>
I'm currently working on my first (small) PHP project going step by step teaching myself. It's going good so far but I do have a question about the following code.. and which I should use in which case..
equal:
if ($word1 != $word2) {
echo "Your words do not match, please go back and correct this.";
die(); }
identical:
if ($word1 !== $word2) {
echo "Your words do not match, please go back and correct this.";
die(); }
Th code runs fine with both of these but I would still like a detailed explanation as to when use which one, for future references, and to learn.
Thank you!
You can understand the difference between them by looking at Types comparison table in PHP manual.
Main difference is that !== is strict about type of compared values while != is weaker check.
the one will pass the other one will not the frist one cheks only for equal the second one checks and for type of var. The var $word1 is string
the $word2 is a integer
if ($word1 != $word2) {
echo "Your words do not match, please go back and correct this.";
}
//with this if stament will pass the test without echo out nothing.
if ($word1 !== $word2) {
echo "Your words do not match, please go back and correct this.";
} //this one will not pass and will echo out your string
?>
I've been working on a very basic search engine. It basically operates by checking if the word exists. If it does, it returns the link. I know most of you would suggest to create a database from phpMyAdmin but I don't remember the password to make the mySql_Connect command work.
Anyway here is the code:
<?php
session_start();
$searchInput = $_POST['search'];
var_dump($inputPage1);
var_dump($searchİnput);
$inputPage1 = $_SESSION['pOneText'];
$inputPage2 = isset($_SESSION['pTwoText']) ? $_SESSION['pTwoText'] : "";
$inputPage3 = isset($_SESSION['pThreeText']) ? $_SESSION['pThreeText'] : "";
if (strpos($inputPage1, $searchInput)) {
echo "True";
} else {
echo "False";
}
?>
When I search a word, any word from any page, weather it exists or not, it always returns false. Does anyone know why?
From the PHP documentation:
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 the function returns the integer 0 since $searchInput starts at the first character of $inputPage1. Since it is inside an if condition, that expects a boolean, the integer is then converted to one. When converted to boolean, zero is equal to false so instead the else block is executed.
To fix it, you need to use the !== operator (the not equal equivalent of ===):
if (strpos($inputPage1, $searchInput) !== false) {
//...
Try stripos() to match case insensitive
First print all items in $_POST and $_SESSION using
echo "<pre>";
print_r($_POST);
print_r($_SESSION);
and ensure that the search string really exist in the bigger string .
Also make sure that your are using "false" to compare :
i.e
$pos = strpos($biggerString,$seachString);
if($pos !== false)
{
echo "Not found";
}
I am getting an array, and I filter it to get just the text, in the text I am looking for something like "20/" Making sure the 20/ exist then if it does, it will go to another part of the code BUT I can't seem to figure out how to get it recognized if its a 1-9/ to 20/.
if ($spot = strpos($dir[$x]->output, '9/')) {
echo "Valid";
}
else {
gotto2();
}
So, it doesn't ever find it, but if I remove the number, it'll find the "/".
your if condition is all wrong.
try this,
if (false !== strpos($dir[$x]->output, '9/')) {
echo "Valid";
}
your doing assignment to $spot ( single = ) even a double == is not suffencient to check because if the position is 0, you need to check for Boolean false strictly ( or in this case true, but we dont care about the position so valid is 0 to any pos ) and we cant look for boolen true , so we check for anything but boolean false
if this doen't work you will have to post the input string as well.
I've a variable which contains comma separated strings. This is generated dynamically from the array elements by using implode() function.So sometimes it contains nothing, sometimes it contains 1/2/3/4 strings separated by comma. I want to check whether the stirng Other is present within this comma separated string and if it's present then do the commands written inside if. But I'm facing a issue in which it's never detecting the string "Other" inside the comma separated values though tit's present. Can anyone help me in this regard please?
For your reference following is my code:
$form_data['que_issue'] = implode(",", $request['que_issue']);
if(strpos($form_data['que_issue'],"Other")) {
echo "In If";
die;
if(!$this->mValidator->validate($form_data['que_issue_comment'], "required", "true"))
$this->mValidator->push_error($errors_msgs['que_issue_comment_blank'], 'que_issue_comment');
elseif(!$this->mValidator->validate($form_data['que_issue_comment'], 'maxlength', '100'))
this->mValidator->push_error($errors_msgs['que_issue_comment_length_invalid'], 'que_issue_comment');
} else
echo "In a else";
die;//Its always going in else part only
Thanks in advance.
strpos will return 0 if string match at the begin of the string. But 0 is interpreted as false by PHP.
Always look the documentation and see what is the specific return type/value when the method "fail" and compare against that. In this case
if(strpos($form_data['que_issue'],"Other") !== false)
you should try something like this
if (strpos($form_data['que_issue'],'Other') !== false) {