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.
Related
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:
What is the meaning of three dots (...) in PHP?
(9 answers)
Closed 5 years ago.
I'm learning PHP http://php.net/manual/en/migration70.new-features.php and in the following example, I don't understand ... prepended with the $ints parameter in the function definition.
<?php
// Coercive mode
function sumOfInts(int ...$ints)
{
return array_sum($ints);
}
var_dump(sumOfInts(2, '3', 4.1));
Can anybody please tell me what those dots are for?
Thanks.
that means that when you call that function, you can pass X integers and the function will process them, doesn't matter how many are they. If you call sumOfInts(3,4,6,2,9) or sumOfInts(3,2,9), the function works, no matter how many arguments you pass
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?
This question already has an answer here:
"Unknown modifier 'g' in..." when using preg_match in PHP?
(1 answer)
Closed 7 years ago.
I have a script I wrote to scan several websites for a google link to make sure it is there. For some reason my script is not working. When I check it at http://www.regexr.com/, it works, but not in live implementation.
example of a link its supposed to find:
https://plus.google.com/+VincentsHeatingPlumbingIncPortHuronTownship/about?hl=en
preg_match I am using:
if (preg_match_all("/(.*)plus.google.com(.*)/", $attributeValue->value))
{
$googleLinkCount ++;
$googleLinkHrefs[] = $attributeValue->value;
}
Don't use a regular expression, use parse_url:
if (parse_url($attributeValue->value, PHP_URL_HOST) === 'plus.google.com') {
// host is plus.google.com
}
This question already has answers here:
If url is /store/*
(3 answers)
Closed 8 years ago.
if($_SERVER['REQUEST_URI']=='/store/'.*.'/aproduct')
Is that possible? So if I type /store/something/aproduct or /store/abc123/aproduct the if statement happens? The '*' should be everything. Dosent matter if you type cow, 123, lol or youarenice
Try with:
if ( preg_match('~^/store/[^/]+/aproduct$~', $_SERVER['REQUEST_URI']) )
yes, you can use regular expressions for that. Something like this should work:
if(preg_match('#^\/store\/.+\/aproduct#', $_SERVER['REQUEST_URI'])) {
// do something
}