php preg_match expression to detect valid facebook post and video urls - php

How can I detect if a) the link is valid (structurally) and b) if it is a valid facebook photo or video link? Looking for the regular expressions for each case, not to determine if it's accessible or a valid destination.
Example photo link:
https://www.facebook.com/949pediatricdentistry/photos/a.1438335623065047.1073741827.1438300469735229/1866032310295374/?type=3&theater
Example video link:
https://www.facebook.com/chevrolet/videos/10153947517247296/
I've tried the following preg_match() statement which is close to detecting the different photo urls, but not fully passing the test:
preg_match('^(http(?:s?)?://www\.facebook\.com/(?:photo\.php\?fbid=\d+|([A-z0-9\.]+)\/photos(?:\/[0-9A-z].+)?\/(\d+)(?:.+)))?', 'https://www.facebook.com/photo.php?fbid=10201485039580806&set=a.2923144830611.2133032.1020548245&type=3&theater');
preg_match('^(http(?:s?)?://www\.facebook\.com/(?:photo\.php\?fbid=\d+|([A-z0-9\.]+)\/photos(?:\/[0-9A-z].+)?\/(\d+)(?:.+)))?', 'https://www.facebook.com/949pediatricdentistry/photos/a.1438335623065047.1073741827.1438300469735229/1866032310295374/?type=3&theater');

You can try to check the host + facebook account + video/photo routine:
<?php
$link = 'https://www.facebook.com/949pediatricdentistry/photos/a.1438335623065047.1073741827.1438300469735229/1866032310295374/?type=3&theater';
$faceLink = explode('/', $link);
if (($faceLink[2] == 'www.facebook.com' && $faceLink[3] == $username)) {
//IS VALID
if ($faceLink[4] == 'photos') {
//photos routine
} else if ($faceLink[4] == 'videos') {
//videos routine
}
}
?>
Try by yourself before ask into Stack.

Related

PHP Check for HTTP and HTTPS in the submitted URL through FORM

I have this code which checks for http:// in the URL submitted. But I want it to also check for https://. So I tried with an or in the if condition but it still checks only for http:// and not https://.
Here is my code.
if(!preg_match("#^http://#i",$turl) or !preg_match("#^https://#i",$turl)){
$msg = "<div class='alert alert-danger'>Invalid Target URL! Please input a standard URL with <span class='text-info'>http://</span> for example <span class='text-info'>http://www.kreatusweb.com</span> </div>";
}
If I now put https:// in the URL and submit, it still returns this error message as now http:// is false here. What logic or code should I use here to check for both. I just don't want users to submit www.somewebsite.com. I want them to submit full URL using either http:// or https://. If either of these two exists in the URL then only the form will be processed further.
You can simplify the regex so the s is optional by just adding a ? after it.
if(!preg_match("#^https?://#i",$turl)){
replace the or with &&
if(!preg_match("#^http://#i",$turl) && !preg_match("#^https://#i",$turl))
I used to do this logic mistake when I started to code because you think like this if (not something or not somethingelse)
but doing if (!http || !https) will return true in both http and https because
1- if it is http, then the !https part will return true
2- if it is https, then the !http part will return true too
Check out the PHP validate filters at http://php.net/manual/en/filter.filters.validate.php.
<?php
$arr = [ 'http:example.com','https:/example.com','https://www.example.com','http://example.com',
'ftp://example.com','www.example.com','www.example.com/test.php','https://www.example.com/test.php?q=6'];
foreach ($arr as $str) {
$filtered = filter_var($str,FILTER_VALIDATE_URL,FILTER_FLAG_SCHEME_REQUIRED|FILTER_FLAG_HOST_REQUIRED);
if (!empty($filtered)) {
if (stripos($filtered,'http') === 0) {
echo $str.' is valid'.PHP_EOL;
} else {
echo $str.' is a valid URL, but not HTTP'.PHP_EOL;
}
} else {
echo $str.' is not a valid URL'.PHP_EOL;
}
}

How to return the host name of the video from a URL using regex in php?

There are two urls:
1- http://www.facebook.com/?v=107084586333124'
2- https://www.youtube.com/watch?v=Ws_RjMYE85o
As you can see, both links contains the ?v=..............
Im using a function to retrieve the video ID and the name of the host (youtube, facebook, etc).
Im using this function to get both id and host name
function get_video_id($url){
$video_id = "";
//YOUTUBE
if(preg_match('#(?<=(?:v|i)=)[a-zA-Z0-9-]+(?=&)|(?<=(?:v|i)\/)[^&\n]+|(?<=embed\/)[^"&\n]+|(?<=(?:v|i)=)[^&\n]+|(?<=youtu.be\/)[^&\n]+#', $url, $videoid)){
if(strlen($videoid[0])) {
$video_id = 'youtube:_:'.$videoid[0];
}
}
//VIMEO
if(preg_match('#(https?://)?(www.)?(player.)?vimeo.com/([a-z]*/)*([0-9]{6,11})[?]?.*#', $url, $videoid)){
if(strlen($videoid[5])) {
$video_id = 'vimeo:_:'.$videoid[5];
}
}
// Facebook
if(preg_match("~/videos/(?:t\.\d+/)?(\d+)~i", $url, $videoid)){
if(strlen($videoid[0])) {
$video_id = 'facebook:_:'.$videoid[1];
}
}
return $video_id;
}
$exp = explode(':_:',get_video_id($_POST['video_url']));
echo $exp[0] .'=>'.$exp[1];
$exp[0] should return the host name (youtube, vimeo, facebook ....etc);
and $exp[1] return the video id.
The function is working fine but the problem I encounter is that when I submit a facebook video link which contains the ?v=
(eg. http://www.facebook.com/?v=107084586333124')
it always returns youtube as a host name. unlike when I submit a link like this one:
https://www.facebook.com/LadBlab/videos/540736926073557/
it return facebook and thus working fine.
How to check if the url is a facebook video or not when a user submit a link like this one and not confuse it with youtube?
http://www.facebook.com/?v=107084586333124'
You can use something like this
$url = 'http://facebook.com/?v=4654654';
if(strpos($url, 'facebook') != FALSE) {
//facebook link
} else if(strpos($url, 'youtube') != FALSE) {
//youtubelink
} else {
//someother link
}
And then apply your preg_match to each link separately to get the video id.

Validate a URL PHP

I've working on a project and in this project i need to check the user input is a valid URL.
I've made a preg_match for all possible characters used on a URL. However, I'm trying to make it show an error if HTTP:// / HTTPS:// is not in front of the URL.
Here is what I've done.
if(preg_match('/[^0-9a-zA-Z.\-\/:?&=#%_]/', $url) || substr($url, 0, 7) != "http://" || substr($url, 0, 8) != "https://") {
But that doesn't work. It keeps giving me the an OK message. I'm not sure what I'm doing wrong here, I hope I can get some help!
The if statement will return true or false. So
if(preg_match('/[^0-9a-zA-Z.\-\/:?&=#%_]/', $url) || substr($url, 0, 7) != "http://" || substr($url, 0, 8) != "https://") {
echo "true";
} else {
echo "false";
}
I just need to check if the url has entered a valid url. I don't need to verify it. Just need to check if it has HTTP:// or HTTPS:// and contains valid URL characters.
Instead of a regex, you could make things easy on yourself and use the URL filtering in filter_var:
if (filter_var($url, FILTER_VALIDATE_URL)) { ...
Alternately you can do this without regex. Though you do also need to validate the url imagine http://">bla</a><script>alert('XSS');</script> as the value passed as there url
<?php
$url = 'http://example.com';
if(in_array(parse_url($url, PHP_URL_SCHEME),array('http','https'))){
if (filter_var($url, FILTER_VALIDATE_URL) !== false) {
//valid url
}else{
//not valid url
}
}else{
//no http or https
}
?>
parse_url()
filter_var()
You've not shown your complete relevant code. So, not sure, why it is not working for you but for url validation, you can check for a detailed discussion on the thread link below:
PHP validation/regex for URL
To validate user input with website url it is good to allow with or without scheme and with or without www, then in view add scheme to set as external url.
$withWww = 'www.' . str_replace(array('www.'), '', $value);
$withScheme = 'http://' . str_replace(array('http://', 'htttps://'), '', $withWww);
$headers = #get_headers($withScheme);
if (strpos($headers[0], '200') === false) {
return false;
}

Regular expression for validation of a facebook page url

I need to validate the facebook page url which should not consider http/https or www given or not?
I mean the following should be accepted or valid:
www.facebook.com/ABCDE
facebook.com/ABCDE
http://www.facebook.com/ABCDE
https://www.facebook.com/ABCDE
And following should not be accepted or invalid:
http://www.facebook.com/ => User name/page name not given
http://www.facebook.com/ABC => User name/page name should have the minimum length of 5.
For the above requirement I'd made following regular expression, but it is not checking the User Name or Page Name which is the only problem. Rest is working fine:
/^(https?:\/\/)?((w{3}\.)?)facebook.com\/(([a-z\d.]{5,})?)$/
I am very new to Regular Expression, so don't have much idea about it.
Any type of help would be appreciable.
Thanks in advance.
parse_url() can help you with that.
<?php
$array = array(
"www.facebook.com/ABCDE",
"facebook.com/ABCDE",
"http://www.facebook.com/ABCDE",
"https://www.facebook.com/ABCDE",
"http://www.facebook.com/",
"http://www.facebook.com/ABC"
);
foreach ($array as $link) {
if (strpos($link, "http") === false) {
$link = "http://" . $link; //parse_url requires a valid URL. A scheme is needed. Add if not already there.
}
$url = parse_url($link);
if (!preg_match("/(www\.)?facebook\.com/", $url["host"])) {
//Not a facebook URL
echo "FALSE!";
}
elseif (strlen(trim($url["path"], "/")) < 5) {
//Trailing path (slashes not included) is less than 5
echo "FALSE!";
}
else {
//None of the above
echo "TRUE";
}
echo "<br>";
}
Try this one (have not tested it, should work)
'~^(https?://)?(www\.)?facebook\.com/\w{5,}$~i'
\w is like [a-zA-Z0-9_]
Robert

parsing url - php, check if scheme exists, validate url regex

Is this a good way to validate a posted URL?
if (filter_var($_POST['url'], FILTER_VALIDATE_URL)){
echo "valid url";
}else{
echo "invalid url";
}
This is what I wrote to start with, as I could show multiple error messages with it:
function validateURL($url)
{
$pattern = '/^(([\w]+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?#)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/';
return preg_match($pattern, $url);
}
$result = validateURL($_POST['url']);
if ($result == "1"){
$scheme = parse_url($_POST['url'], PHP_URL_SCHEME);
if (isset($scheme)){
echo $scheme . "://" . parse_url($_POST['url'], PHP_URL_HOST);
}else{
echo "error you did not enter http://";
}
}else{
echo "your url is not a valid format";
}
I'd simply go for the build-in FILTER_VALIDATE_URL and use a generic error message like:
Invalid URL. Please remember to input http:// as well.
If you're nice you could check if the first 7/8 letters are http:// or https:// and prepend them if not.
Coming up with and maintaining such a RegEx is not something you should get into if the problem is already solved. There's also usually no need to be any more detailed in the error message, unless you're in the business of explaining URL formats.
Have you checked this out Kyle, http://phpcentral.com/208-url-validation-in-php.html
I think simple
filter_var($var, FILTER_VALIDATE_URL)
and checking the protocol by strpos() is enough because user can, if wants to, give you the wrong (which does not exists) url.
Of course you can check if domain exists and return valid http status but I think it is little overstatement.

Categories