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
Related
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.
Situation is getting a logo on:
domain.com/special_dir/any_page
or
domain.com/special_dir/any_dir/
to use a link to [domain.com/special_dir/].
Everywhere else on [domain.com/] the logo must a link to [domain.com/]
This is what I have so far.
<?php
$host = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
if( $host == 'domain.com/special_dir/' ) {
echo '<div"><img src="..."></div>';
} else {
echo '<div"><img src="..."></div>';
}
?>
The logo for [domain.com/special_dir/] only works for [domain.com/special_dir/] URL, no others. I suppose the code it doing what it should, I just don't know how to make it recursive. I did search and read a lot of similar situations but none based on PHP code worked for me.
It is WordPress Multi-site setup and the "special_dir" is a regular sub-directory.
How to correct?
Thanks
Your if ($host == 'domain.com/special_dir/') statement means the special link will be printed for domain.com/special_dir/ only. It excludes everything else, including comain.com/special_dir/any_dir and domain.com/special_dir/any_page.
If think you want ...
if (substr($host,0,22) == 'domain.com/special_dir/') { ... }
This did the trick.
<?php
$url = $_SERVER["REQUEST_URI"];
if (strpos($url, "/special_dir/") === 0) {
echo '<div"><img src="..."></div>';
} else {
echo '<div"><img src="..."></div>';
}
?>
I use:
if (filter_var($_GET['paste_here'], FILTER_VALIDATE_URL)) {
echo ???;
}
And I'd like as soon as user enters a site after .php?paste_here = that specific site to be displayed on echo. But I don't want how to print. Any ideas? Thanks a lot
You should check out if param exists, so:
if (isset($_GET['paste_here']) && filter_var($_GET['paste_here'], FILTER_VALIDATE_URL)) {
echo $_GET['paste_here'];
}
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.
I want to have the regular expression that makes sure the beginning of the string contains 'http://' and '/' and the end.
This is a longer version I came up with,
if(!preg_match("/(^http:\/\//", $site_http))
{
$error = true;
echo '<error elementid="site_http" message="site_http - Your link appears to be invalid. Please confirm that your link contains http:// at the start."/>';
}
elseif (!preg_match("/\/$/", $site_http))
{
$error = true;
echo '<error elementid="site_http" message="site_http - Your link appears to be invalid. Please confirm that your link has ended with a /."/>';
}
but I thought these two expressions can put together like below, but it wont work,
if(!preg_match("/(^http:\/\/)&(\/$)/", $site_http))
{
$error = true;
echo '<error elementid="site_http" message="site_http - Your link appears to be invalid. Please confirm that your link contains http:// at the start and a / at the end."/>';
}
the multiple expressions that I try to combine must be wrong! any idea?
thanks,
Lau
if(preg_match('/^http:\/\/.*\/$/', $site_http))
{
...
}
The ^http:\/\/ forces http:// at the front, the \/$ forces a slash at the end, and .* allows everything (and possibly nothing) between.
For example:
<?php
foreach (array("http://site.com.invalid/", "http://") as $site_http) {
echo "$site_http - ";
if (preg_match('/^http:\/\/.*\/$/', $site_http)) {
echo "match\n";
}
else {
echo "no match\n";
}
}
?>
generates the following output:
http://site.com.invalid/ - match
http:// - no match