php comparison function not working as expected - php

I'm trying to check whether the entered text is URL or string. My goal is i should get "URL Detected" if entered text is www.yout.com,http://stackoverflow.com, etc
if (stripos($text, ".com") !== false) {
echo "URL Detected";
}

you mast use filter_var
$incomingData = "www.google.com";
$url = filter_var($incomingData, FILTER_VALIDATE_URL);
if ($url !== false) {
echo "URL Detected";
}

Use regular expression to check if it's an url.
For example :
$regex = "/[-a-zA-Z0-9#:%_\+.~#?&//=]{2,256}\.[a-z]{2,4}\b(\/[-a-zA-Z0-9#:%_\+.~#?&//=]*)?/"
if(preg_match( $regex , "www.google.com" ){
echo "URL Detected"
}

Related

PHP url preg_match

I have a problem with this preg_match
function isValidURL($url){
return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}
if(!isValidURL($url)){
echo 'false';
} else
echo 'true';
For these links should display - true
/test.html
/testowa-strona_9067.html
/567890.html?get=test
/costam.html?get=2&f[]=k&f[]=k2
And for those false
/.html
/ąęśćzmn-ż.html
/testmhtml
/%67%68%89(i&.html?get=34
But it always displays true
You should try this :
if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) {
die('Not a valid URL');
}

Check if URL has certain string and match position with PHP

I've tried the following:
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,'austin') !== false) {
echo 'Austin metro';
} else if (strpos($url,'georgetown') !== false) {
echo 'Georgetown';
}
The issue is that this will match a URL that has austin anywhere.
For example, the url for georgetown parameter is like this: www.domain.com/austin/georgetown/
So if url has austin/georgetown, would like to display georgetown option.
Any suggestions? Thanks!
Go like this:
$url = 'www.domain.com/austin/georgetown'; //output: georgetown
if (strpos($url,'georgetown') !== false && strpos($url,'austin') !== false)
{
echo 'Georgetown';
}
else if (strpos($url,'georgetown') !== false)
{
echo 'Georgetown';
}
else if (strpos($url,'austin') !== false)
{
echo 'austin';
}
first if condition is checking if austin and georgetown, both are there in the url, if yes georgetown will be printed. rest two conditions are just checking for austin and georgetown individually.
See hear Php fiddle -> https://eval.in/715874
Hope this helps.

Check if URL has an exact string with PHP

I've tried the following:
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if (strpos($url,'car') !== false) {
echo 'Car exists.';
} else {
echo 'No cars.';
}
However if the url contains the word "care" or "car2" it will also trigger "Car Exists". How do I get a match if only "car" is found?
Change
if (strpos($url,'car') !== false) {
into
if (preg_match('/\bcar\b', $url) !== 0) {
Basically, you search for word car with no other alphanumeric surrounding it.
Hope this helps!

How to validate Vine URL and allow HTTP or HTTPS using PHP

How can this be changed to allow either HTTP or HTTPS for a Vine URL?
$vineURL = 'https://vine.co/v/';
$pos = stripos($url_input_value, $vineURL);
if ($pos === 0) {
echo "The url '$url' is a vine URL";
}
else {
echo "The url '$url' is not a vine URL";
}
You can use the parse_url function, it breaks the URL into its components making it easier to match each component individually:
var_dump(parse_url("https://vine.co/v/"));
// array(3) {
// ["scheme"]=>
// string(4) "http"
// ["host"]=>
// string(7) "vine.co"
// ["path"]=>
// string(3) "/v/"
// }
You can then just check if scheme, host and path match:
function checkVineURL($url) {
$urlpart = parse_url($url);
if($urlpart["scheme"] === "http" || $urlpart["scheme"] === "https") {
if($urlpart["host"] === "vine.co" || $urlpart["host"] === "www.vine.co") {
if(strpos($urlpart["path"], "/v/") === 0) {
return true;
}
}
}
return false;
}
checkVineURL("https://vine.co/v/"); // true
checkVineURL("http://vine.co/v/"); // true
checkVineURL("https://www.vine.co/v/"); // true
checkVineURL("http://www.vine.co/v/"); // true
checkVineURL("ftp://vine.co/v/"); // false
checkVineURL("http://vine1.co/v/"); // false
checkVineURL("http://vine.co/v1/"); // false
Just take out the "https://" and change your if statement a bit... like this:
$vineURL = 'vine.co/v/';
if(stripos($user_input_value, $vineURL) !== false) {
echo "This is a vine URL";
} else {
echo "This is not a vine URL";
}
User RegEx like this
if (preg_match("/^http(s)?:\/\/(www\.)?vine\.co\/v\//", $url)) {
echo "This is a vine URL";
} else {
echo "This is not a vine URL";
}

PHP - Check if URL exists in link

Here is what I want to do..
Lets say I am looking for the link "example.com" in a file at http://example.com/test.html".
I want to take a PHP script that looks for an in the mentioned website. However, I also need it to work if there is a class or ID tag in the <A>.
See below url
How can I check if a URL exists via PHP?
or try it
$file = 'http://www.domain.com/somefile.jpg';
$file_headers = #get_headers($file);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
$exists = false;
}
else {
$exists = true;
}
From here: http://www.php.net/manual/en/function.file-exists.php#75064
...and right below the above post, there's a curl solution:
function url_exists($url) {
if (!$fp = curl_init($url)) return false;
return true;
}
Update code:-
You can use SimpleHtmlDom Class for find id or class in tag
see the below URL
http://simplehtmldom.sourceforge.net/
http://simplehtmldom.sourceforge.net/manual_api.htm
http://sourceforge.net/projects/simplehtmldom/files/
http://davidwalsh.name/php-notifications
Here is what I have found in case anyone else needs it also!
$url = "http://example.com/test.html";
$searchFor = "example.com"
$input = #file_get_contents($url) or die("Could not access file: $url");
$regexp = "<a\s[^>]*href=(\"??)([^\" >]*?)\\1[^>]*>(.*)<\/a>";
if(preg_match_all("/$regexp/siU", $input, $matches, PREG_SET_ORDER)) {
foreach($matches as $match) {
echo $match[2];
if ($match[2] == $searchFor)
{
$isMatch = 1;
} else {
$isMatch= 0;
}
// $match[0] = A tag
// $match[2] = link address
// $match[3] = link text
}
}
if ($isMatch)
{
echo "<p><font color=red size=5 align=center>The page specified does contain your link. You have been credited the award amount!</font></p>";
} else {
echo "<p><font color=red size=5 align=center>The specified page does not have your referral link.</font></p>";
}

Categories