Here is my code:-
<?php
$URL = "http://www.mylink.com/show_album/XXXXX";
/*The value of $URL will be passed from previous page via Form*/
?>
First, The value of the variable must be a link/URL. If not, echo "Not an URL"; should be executed. But, I am unable to check if it's a link or not. Please, anyone help me to figure out this. As a newbie, I need help badly.
Second, The http:// is optional. If provided, it's okay. Else, "http://" will be added automatically. I used this code for this and it works.
if(strpos($URL, "http://") !== false){
echo $URL;
echo "<br />http:// already provided";
return $URL;
}
else {
echo "http://" . $URL;
$URL = "http://" . $URL;
echo "<br />http:// automatically added";
return $URL;
}
Third, The text "mylink.com/show_album/" (without quote) must be available in $URL. I used this code for this:-
if (strpos($URL, "mylink.com/show_album/") !== false){
echo "The "mylink.com/show_album/ is available";
}
else {
echo "The "mylink.com/show_album/ is not available";
}
It's okay.
Next, In the $URL, the value is a link name with a last word XXXXX. It must be the last word of this variable. XXXXX is a number. It can be any number like "12345" or any other. But, I want to get the Number from this $URL and show it. How can I do this?
At last, The link can only contain http:// and www.mylink.com/show_album/XXXXX . Otherwise, the echo "Not a valid link"; will be executed. How to do this?
For Validating URLs:
$text = preg_replace("
#((http|https|ftp)://(\S*?\.\S*?))(\s|\;|\)|\]|\[|\{|\}|,|\"|'|:|\<|$|\.\s)#ie",
"'$3$4'",
$text
);
For validating/appending http:// on front of URL, do something like:
$protocol = substr($URL, 0, 7);
if($protocol!="http://")
{
$URL="http://".$URL;
}
EDIT
Now for fetching the last numbers:
$XXXXX=substr( $URL, strrpos( $url, '/' )+1 );
Related
i'm new at this forum and php.
I want to show some info when the script detects one or more words in the postname (Wordpress)
In my exapmle like to dispaly extra info when Omnik + reset or wifi is detected.
I like to know how i can simplify the following code:
$url = "www.myurl.nl/postname"
if (strpos($url, 'omnik' )!==false){
echo "Omnik";
}
else if (strpos($url, 'reset' )!==false){
echo "Reset";
}
else if (strpos($url, 'wifi' )!==false){
echo "Wifi";
}
else {
echo "No Omnik,Reset or Wifi there";
}
At this moment i can only show the extra info when the word "Omnik" is detected.
Example: https://geaskb.nl/omnik shows the extra info, but https://geaskb.nl/omnik-reset and https://geaskb.nl/omnik-wifi should show the info too, while https://geaskb.nl/solaredge shouldn't show the info.
Hope you get what i mean.
====== Added 20:00 ========
Hi All, thanks for the ansewers.
I should have be clear the 1st time i guess.
This is the code i use now:
// Verkrijg URL incl. subdir.
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on')
$geturl= "https";
else
$geturl = "http";
// Here append the common URL characters.
$geturl .= "://";
// Append the host(domain name, ip) to the URL.
$geturl .= $_SERVER['HTTP_HOST'];
// Append the requested resource location to the URL
$geturl .= $_SERVER['REQUEST_URI'];
if (strpos($_SERVER['REQUEST_URI'], "omnik" )!==false){
echo "Omnik in url";
}
else {
echo "Geen Omnik in $geturl";
}
============= 20:30u ==================
Problem solved!! stripos solved the problem!
Thanks for all your help!
One way for doing it through foreach loop
<?php
$url = "www.myurl.nl/postname";
$needles = ['omnik', 'reset', 'wifi']; // Add more if needed
foreach($needles as $needle){
if (strpos($url, $needle )!==false){
echo $needle;
}
}
?>
Note: I'm using an older PHP version so FILTER_VALIDATE_URL is not available at this time.
After many many searches I am still unable to find the exact answer that can cover all URL structure possibilities but at the end I'm gonna use this way:
I'm using the following function
1) Function to get proper scheme
function convertUrl ($url){
$pattern = '#^http[s]?://#i';
if(preg_match($pattern, $url) == 1) { // this url has proper scheme
return $url;
} else {
return 'http://' . $url;
}
}
2) Conditional to check if it is a URL or not
if (preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&##\/%?=~_|!:,.;]*[-a-z0-9+&##\/%=~_|]/i", $url)) {
echo "URL is valid";
}else {
echo "URL is invalid<br>";
}
Guess What!? It works so perfect for all of these possibilities:
$url = "google.com";
$url = "www.google.com";
$url = "http://google.com";
$url = "http://www.google.com";
$url = "https://google.com";
$url = "https://www.codgoogleekarate.com";
$url = "subdomain.google.com";
$url = "https://subdomain.google.com";
But still have this edge case
$url = "blahblahblahblah";
The function convertUrl($url) will convert this to $url = "http://blahblahblahblah";
then the regex will consider it as valid URL while it isn't!!
How can I edit it so that it won't pass a URL with this structure http://blahblahblahblah
If you want to validate internet url's, add a check for including a dot (.) character in your reg-ex.
Note: http://blahblahblah is a valid url as is http://localhost
Try this:
if (preg_match("/^(([\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})*)?$/", $url)) {
echo "URL is valid";
}else {
echo "URL is invalid<br>";
}
I have a form to get some urls from users. Eg: Web Address, Facebook Address, Twitter Address, Google+ address etc... My problem is how I validate these urls when they submit the form. I tried to validate URL in PHP by using the FILTER_VALIDATE_URL or simply, using regular expression.
Here, I would like to know what are the best methods to get such a urls from users. Is it always good to let them to enter protocol? sometimes they may not know it is http, https, ftp, ftps.. etc. I think it is something hard to do some users.
I tried something like this using FILTER_VALIDATE_URL, But it always use protocol and sometime I am confusing how its work..
// validate url
$url = 'http://www.example.com';
if (filter_var( $url, FILTER_VALIDATE_URL)){
echo "<br>valid";
} else {
echo "<br>invalid";
}
OUTPUT : valid
// validate url
$url = 'hp://www.example.com';
if (filter_var( $url, FILTER_VALIDATE_URL)){
echo "<br>valid";
} else {
echo "<br>invalid";
}
OUTPUT : valid
// validate url
$url = 'http://example.com';
if (filter_var( $url, FILTER_VALIDATE_URL)){
echo "<br>valid";
} else {
echo "<br>invalid";
}
OUTPUT : valid
// validate url
$url = 'http://example.com?id=32&name=kamalani';
if (filter_var( $url, FILTER_VALIDATE_URL)){
echo "<br>valid";
} else {
echo "<br>invalid";
}
OUTPUT : valid
Can you tell me what are the best ways to get urls from user and how those validate?
Any comments are greatly appreciating..
Thank you.
You need to use regular expression to check valid url here.
Please try this :
$pattern = "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&##\/%?=~_|!:,.;]*[-a-z0-9+&##\/%=~_|]/i";
$URL= 'http://example.com?id=32&name=kamalani';
if(preg_match($pattern, $URL) ){
echo "<br>valid";
} else{
echo "<br>invalid";
}
Output
valid
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
How can I add http:// to a URL if it doesn't already include a protocol (e.g. http://, https:// or ftp://)?
Example:
addhttp("google.com"); // http://google.com
addhttp("www.google.com"); // http://www.google.com
addhttp("google.com"); // http://google.com
addhttp("ftp://google.com"); // ftp://google.com
addhttp("https://google.com"); // https://google.com
addhttp("http://google.com"); // http://google.com
addhttp("rubbish"); // http://rubbish
A modified version of #nickf code:
function addhttp($url) {
if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
$url = "http://" . $url;
}
return $url;
}
Recognizes ftp://, ftps://, http:// and https:// in a case insensitive way.
At the time of writing, none of the answers used a built-in function for this:
function addScheme($url, $scheme = 'http://')
{
return parse_url($url, PHP_URL_SCHEME) === null ?
$scheme . $url : $url;
}
echo addScheme('google.com'); // "http://google.com"
echo addScheme('https://google.com'); // "https://google.com"
See also: parse_url()
Simply check if there is a protocol (delineated by "://") and add "http://" if there isn't.
if (false === strpos($url, '://')) {
$url = 'http://' . $url;
}
Note: This may be a simple and straightforward solution, but Jack's answer using parse_url is almost as simple and much more robust. You should probably use that one.
The best answer for this would be something like this:
function addhttp($url, $scheme="http://" )
{
return $url = empty(parse_url($url)['scheme']) ? $scheme . ltrim($url, '/') : $url;
}
The protocol flexible, so the same function can be used with ftp, https, etc.
Scan the string for ://. If it does not have it, prepend http:// to the string... Everything else just use the string as is.
This will work unless you have a rubbish input string.
Try this. It is not watertight1, but it might be good enough:
function addhttp($url) {
if (!preg_match("#^[hf]tt?ps?://#", $url)) {
$url = "http://" . $url;
}
return $url;
}
1. That is, prefixes like "fttps://" are treated as valid.
nickf's solution modified:
function addhttp($url) {
if (!preg_match("#^https?://#i", $url) && !preg_match("#^ftps?://#i", $url)) {
$url = "http://" . $url;
}
return $url;
}
<?php
if (!preg_match("/^(http|ftp):/", $_POST['url'])) {
$_POST['url'] = 'http://'.$_POST['url'];
}
$url = $_POST['url'];
?>
This code will add http:// to the URL if it’s not there.