This question already has answers here:
How do I check if a string contains a specific word?
(36 answers)
Closed 7 years ago.
I'd like to check if a string is equal to another string. By equal, I mean that I want to check if the second string includes the first one in order to make mysql join's in a dynamic way. (I hope I'm clear, sorry for my english...)
I've seen some functions as strcmp() but it only checks if it is purely equal.
It's the same as "$var1 === $var2".
Is there a function which can do that ? Or could you give me some leads to do that ?
if (strpos($a,'are') !== false) {
echo 'true';
}
How do I check if a string contains a specific word in PHP?
Related
This question already has answers here:
regex preg_match for digit in middle for a string
(3 answers)
Closed 3 years ago.
This is what I am currently using to check the incoming get request
if (filter_input(INPUT_SERVER, 'REQUEST_METHOD') === 'GET' && strpos($_SERVER['REQUEST_URI'], 'api/rooms/1/messages') !== false) {
}
as you can see the rooms number is hardcoded I only need to check if it is a number at all on that specific spot between rooms/ and /messages how can I achieve this
You could probably use preg_match.
Instead of doing:
strpos($_SERVER['REQUEST_URI'], 'api/rooms/1/messages') !== false
You could do:
preg_match("/api\/rooms\/[0-9]+\/messages/", $_SERVER['REQUEST_URI'])
This question already has answers here:
Simple PHP strpos function not working, why?
(5 answers)
Closed 3 years ago.
Using php 7.1 and I have strange issue with strpos(). We have post value that needs to detect presence of # sign.
Tried:
if(strpos($_POST["text"],"#")>0)
{..} else {..}
and
if(strpos($_POST["text"],"#")!== false)
{..} else {..}
but it always go to else part. Also tried escaping hashtag
if(strpos($_POST["text"],"\#")>0)
Any idea what to do?
Hashtag is first value and I need to know is it present. Example value is:
$_POST["text"]='# 1010';
if(strpos($_POST["text"],"#")!== false)
should be used, not:
if(strpos($_POST["text"],"#")>0)
# could be the first character which is not >0.
This question already has answers here:
Why does PHP consider 0 to be equal to a string?
(9 answers)
Closed 8 years ago.
When I run the follow code in PHP
if('yes' == 0)
echo 'a';
else
echo 'b';
The output is a.
I don't understand what happen?
And can I convert the php code to C source code to have a look what real happening?
PHP is a dynamically typed language, and == is a loose comparison operator, meaning it will first cast values it compares to one type, int for that matter, and then compare them; strings are being cast to integers by taking numericals from the left part, so 1abc casts to 1. By that logic yes cast to 0, and 0 == 0 yields true.
This question already has answers here:
Check if url contains parameters [duplicate]
(3 answers)
How to verify if $_GET exists?
(7 answers)
Closed 9 years ago.
I have lots of PHP statements along the lines of:
$getvalue = $_GET['valueiwant'];
In some scenarios not all variables are available. So, let's say 'valueiwant' doesn't exist in the URL string, how can I return a value based on the fact it doesn't exist?
For example if 'valueiwant' can't be found set $getvalue to -1
Currently it appears the value defaults to 0 and I need to be equal less than 0 if it doesn't exist.
Any ideas?
thanks
I always use
$getvalue=isset($_GET['valueiwant'])?$_GET['valueiwant']:-1;
Use of the isset() function checks if the offset exists, and returns a boolean value indicating it's existence.
This means that you can structure an if statement around the output.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
PHP startsWith() and endsWith() functions
Check if variable starts with ‘http’
How make an if-statement that if $mystring has the prefix "http://" then {do something}.
I've done this in objective-c like this:
if([mynsstring hasPrefix:#"http://"])
{
//Do something...
}
I don't know how to do this in PHP.
Thanks for your help in advance.
Simplest would be using substring to compare
if (substr($mystring, 0, 7) === 'http://') {
// do something
}
Remember of course to take the exact number of characters.