Working with strings - PHP - php

I need some PHP help with strings.
I have a textbox field where users will enter a facebook profile link.
Example: http://facebook.com/zuck
Now the problem is I need to have EXACTLY this string: "http://graph.facebook.com/zuck".
Inputs could be anything like:
http://facebook.com/zuck
http://www.facebook.com/zuck
www.facebook.com/zuck
facebook.com/zuck
What's the best way to do that? Thank you in advance.

To accept anything in the format of facebook.com/username where username is alphanumeric with dots, dashes, and underscores (not sure what Facebook allows exactly):
if (preg_match('%facebook.com/([a-z0-9._-]+)%i', $input, $m))
{
echo 'http://graph.facebook.com/', $m[1], "\n";
}

Why don't you just ask the user for their username? Instead of accepting a wide variety of input, design the form so that they only have to put in their username.
Something along the lines of this;
This way, you don't even have to validate or store anything other than their username. This could be super helpful down the road when Facebook moves fast and breaks stuff, like the URLs of users. Or if you want to form URLs for something other than graph API, you won't have to pull apart an existing URL.

If given inputs will be always as the ones you give i think that strstr function would hadle this
$array = array('http://facebook.com/zuck', 'http://www.facebook.com/buck', 'www.facebook.com/luck', 'facebook.com/nuck');
foreach($array as $data)
{
if(strstr($data, 'facebook.com/'))
{
echo 'http://graph.'.strstr($data, 'facebook.com/') . '<br>';
}
}
This will output
http://graph.facebook.com/zuck
http://graph.facebook.com/buck
http://graph.facebook.com/luck
http://graph.facebook.com/nuck

Find the last slash in the input
$lastpos = strrchr ( $input , '/' )
Manually concatenate the url and everything after that last slash.
$new_url = 'http://www.facebook.com' . substr($input, $lastpos);

$url = 'http://facebook.com/zuck';
$array = explode('/', str_replace('http://', '', $url));
$username = $array[1];
$finalurl = 'http://graph.facebook.com/zuck'.$username;
echo $finalurl;
This will work with any format of input URL.

Something along the lines of:
Pattern:
(https?://)?(www\.)?(.+?)\/([^/]+)
Replace with:
http://graph.$3/$4
Test it here:
http://www.regexe.com/

Related

PHP: How to strip all types of extensions from URL (incl. period)

I am new to PHP and hope someone can help me with this.
I want PHP to give me the name of the current page of my website.
The important thing is that I need this without any leading slashes and without any trailing extensions etc., just the plain page name.
Example:
The URL of a page is http://www.myurl.com/index.php?lang=en
In this case it should only return "index".
I found a way to get rid of the leading part using the following but have trouble to remove the trailing part since this is variable (it can be just .php or .php?lang=en or .php=lang=de etc.).
$pageName = basename($_SERVER["REQUEST_URI"]);
The only thing I found is the following but this doesn't cover the variable extension part:
$pageName = basename($_SERVER["REQUEST_URI"], ".php");
Can someone tell me how to get rid of the trailing part as well ?
Many thanks in advance,
Mike
You can use parse_url in combination with pathinfo:
<?php
$input = 'http://www.myurl.com/index.php?lang=en';
$output = pathinfo(parse_url($input, PHP_URL_PATH), PATHINFO_FILENAME);
var_dump($output); // => index
demo: https://eval.in/382330
One possible way is:
$url = "http://www.myurl.com/index.php?lang=en";
preg_match('/\/([\w-_]+)\.php/i',$url,$match);
echo $match[1];
If you need help with the regex look here:
https://regex101.com/r/cM8sS3/1
here is simplest solution.
$pagename = basename($_SERVER['PHP_SELF']);
$a = explode(".",$pagename);
echo $a[0];
A tutorial on how to do it
With an .htaccess file you can:
Redirect the user to different page
Password protect a specific directory
Block users by IP Preventing hot
linking of your images
Rewrite URIs
Specify your own Error Documents
Try this
//return url
$pageName = base64_decode($_GET["return_url"]);
function Url($pageName) {
$pageName= strtolower($pageName);
$pageName= str_replace('.',' ',$pageName);
$pageName= preg_replace("/[^a-z0-9_\s-]/", "", $pageName);
$pageName= preg_replace("/[\s-]+/", " ", $pageName);
$pageName= preg_replace("/[\s_]/", "-", $pageName);
return $pageName ;
}
$cleanurl=Url($pageName);
echo $cleanurl;
This is a situation where I would just use a regular expression. Here's the code:
$pagename = basename("http://www.myurl.com/index.php?lang=en");
$pagename = preg_replace("/\..*/", "", $pagename);
You can see a working demo here: https://ideone.com/RdrHzc
The first argument is an expression that matches for a literal period followed by any number of characters. The second argument tells the function to replace the matched string with an empty string, and the last argument is the variable to operate on.

Get vine video id using php

I need to get the vine video id from the url
so the output from link like this
https://vine.co/v/bXidIgMnIPJ
be like this
bXidIgMnIPJ
I tried to use code form other question here for Vimeo (NOT VINE)
Get img thumbnails from Vimeo?
This what I tried to use but I did not succeed
$url = 'https://vine.co/v/bXidIgMnIPJ';
preg_replace('~^https://(?:www\.)?vine\.co/(?:clip:)?(\d+)~','$1',$url)
basename maybe?
<?php
$url = 'https://vine.co/v/bXidIgMnIPJ';
var_dump(basename($url));
http://codepad.org/vZiFP27y
Assuming it will always be in that format, you can just split the url by the / delimiter. Regex is not needed for a simple url such as this.
$id = end(explode('/', $url));
Referring to as the question is asked here is a solution for preg_replace:
$s = 'https://vine.co/v/bXidIgMnIPJ';
$new_s = preg_replace('/^.*\//','',$s);
echo $new_s;
// => bXidIgMnIPJ
or if you need to validate that an input string is indeed a link to vine.co :
$new_s = preg_replace('/^(https?:\/\/)?(www\.)?vine\.co.*\//','',$s);
I don't know if that /v/ part is always present or is it always v... if it is then it may also be added to regex for stricter validation:
$new_s = preg_replace('/^(https?:\/\/)?(www\.)?vine\.co\/v\//','',$s);
Here's what I am using:
function getVineId($url) {
preg_match("#(?<=vine.co/v/)[0-9A-Za-z]+#", $url, $matches);
if (isset($matches[0])) {
return $matches[0];
}
return false;
}
I used a look-behind to ensure "vine.co/v/" always precedes the ID, while ignoring if the url is HTTP or HTTPS (or if it lacks a protocol altogether). It assumes the ID is alphanumeric, of any length. It will ignore any characters or parameters after the id (like Google campaign tracking parameters, etc).
I used the "#" delimiter so I wouldn't have to escape the forward slashes (/), for a cleaner look.
explode the string with '/' and the last string is what you are looking for :) Code:
$vars = explode("/",$url);
echo $vars[count($vars)-1];
$url = 'https://vine.co/v/b2PFre2auF5';
$regex = '/^http(?:s?):\/\/(?:www\.)?vine\.co\/v\/([a-zA-Z0-9]{1,13})$/';
preg_match($regex,$url,$m);
print_r($m);
1. b2PFre2auF5

how to parse this url?

if I have this url: node/95/pdf/1. How will I able to get the numeric/value 1? Tried the parse_url but gave me the wrong output.
PS: the value 1 is just an example, the id is dynamic depends on what the user click.
I would use sscanf
Untested example:
list($node_id, $pdf_id) = sscanf($url, "node/%d/pdf/%d");
$node_id contains the node id, $pdf_id contains the pdf id. According to your comment: Yes, you can output it with e.g. echo $pdf_id;.
If you need them both in an array, you can remove the list() method, doing it like this:
$ids = sscanf($url, "node/%d/pdf/%d");.
This returns an array with both node and pdf id in $ids.
Finally, if you just need the pdf id, you could do
$id = sscanf($url, "node/95/pdf/%d");.
I just showed how to fetch both because I assumed you may need both numbers from your url.
Edit
seeing all the other answers after posting my solution, I am wondering why everyone is solving this with multiple functions when there is a function available that does exactly what he needs: parsing a string according to a format. This also leads to less sql-injection prone code IMHO. And it doesn't break something when the url gets extended or query strings are appended.
Edit 2
list($node_id, $sub, $sub_id) = sscanf($url, "node/%d/%[^/]/%d"); will get you the "pdf" and it's id separate instead of "node/%d/%s/%d". This is because char / is also matched by %s. Using %[^/] matches everything except the forward slash.
You can do this:
$id = end(explode('/', 'node/95/pdf/1'));
Example:
$arr = explode('/', 'node/95/pdf/1');
$id = end($arr);
echo $id; // 1
$url = "node/95/pdf/1";
// Find the last occurence of a slash, get everything after that.
$id = substr($url, strrpos($url, "/") + 1 );
Try with:
$input = 'node/95/pdf/1';
$parts = explode('/', $input);
$output = (int) $parts[3];

Determine User Input Contains URL

I have a input form field which collects mixed strings.
Determine if a posted string contains an URL (e.g. http://link.com, link.com, www.link.com, etc) so it can then be anchored properly as needed.
An example of this would be something as micro blogging functionality where processing script will anchor anything with a link. Other sample could be this same post where 'http://link.com' got anchored automatically.
I believe I should approach this on display and not on input. How could I go about it?
You can use regular expressions to call a function on every match in PHP. You can for example use something like this:
<?php
function makeLink($match) {
// Parse link.
$substr = substr($match, 0, 6);
if ($substr != 'http:/' && $substr != 'https:' && $substr != 'ftp://' && $substr != 'news:/' && $substr != 'file:/') {
$url = 'http://' . $match;
} else {
$url = $match;
}
return '' . $match . '';
}
function makeHyperlinks($text) {
// Find links and call the makeLink() function on them.
return preg_replace('/((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:#=.+?,##%&~-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])/e', "makeLink('$1')", $text);
}
?>
You will want to use a regular expression to match common URL patterns. PHP offers a function called preg_match that allows you to do this.
The regular expression itself could take several forms, but here is something to get you started (also maybe just Google 'URL regex':
'/^(((http|https|ftp)://)?([[a-zA-Z0-9]-.])+(.)([[a-zA-Z0-9]]){2,4}([[a-zA-Z0-9]/+=%&_.~?-]))$/'
So your code should look something this:
$matches = array(); // will hold the results of the regular expression match
$string = "http://www.astringwithaurl.com";
$regexUrl = '/^(((http|https|ftp):\/\/)?([[a-zA-Z0-9]\-\.])+(\.)([[a-zA-Z0-9]]){2,4}([[a-zA-Z0-9]\/+=%&_\.~?\-]*))*$/';
preg_match($regexUrl, $string, $matches);
print_r($matches); // an array of matched patterns
From here, you just want to wrap those URL patterns in an anchor/href tag and you're done.
Just how accurate do you want to be? Given just how varied URLs can be, you're going to have to draw the line somewhere. For instance. www.ca is a perfectly valid hostname and does bring up a site, but it's not something you'd EXPECT to work.
You should investigate regular expressions for this.
You will build a pattern that will match the part of your string that looks like a URL and format it appropriately.
It will come out something like this (lifted this, haven't tested it);
$pattern = "((https?|ftp|gopher|telnet|file|notes|ms-help):((//)|(\\\\))+[\w\d:##%/;$()~_?\+-=\\\.&]*)";
preg_match($pattern, $input_string, $url_matches, PREG_OFFSET_CAPTURE, 3);
$url_matches will contain an array of all of the parts of the input string that matched the url pattern.
You can use $_SERVER['HTTP_HOST'] to get the host information.
<?php
$host = $SERVER['HTTP_HOST'];
?>
Post

Removing content from start and end of string (PHP)

I'm trying to get a users ID from a string such as:
http://www.abcxyz.com/123456789/
To appear as 123456789 essentially stripping the info up to the first / and also removing the end /. I did have a look around on the net but there seems to be so many solutions but nothing answering both start and end.
Thanks :)
Update 1
The link can take two forms: mod_rewrite as above and also "http://www.abcxyz.com/profile?user_id=123456789"
I would use parse_url() to cleanly extract the path component from the URL:
$path = parse_URL("http://www.example.com/123456789/", PHP_URL_PATH);
and then split the path into its elements using explode():
$path = trim($path, "/"); // Remove starting and trailing slashes
$path_exploded = explode("/", $path);
and then output the first component of the path:
echo $path_exploded[0]; // Will output 123456789
this method will work in edge cases like
http://www.example.com/123456789?test
http://www.example.com//123456789
www.example.com/123456789/abcdef
and even
/123456789/abcdef
$string = 'http://www.abcxyz.com/123456789/';
$parts = array_filter(explode('/', $string));
$id = array_pop($parts);
If the ID always is the last member of the URL
$url="http://www.abcxyz.com/123456789/";
$id=preg_replace(",.*/([0-9]+)/$,","\\1",$url);
echo $id;
If there is no other numbers in the URL, you can also do
echo filter_var('http://www.abcxyz.com/123456789/', FILTER_SANITIZE_NUMBER_INT);
to strip out everything that is not a digit.
That might be somewhat quicker than using the parse_url+parse_str combination.
If your domain does not contain any numbers, you can handle both situations (with or without user_id) using:
<?php
$string1 = 'http://www.abcxyz.com/123456789/';
$string2 = 'http://www.abcxyz.com/profile?user_id=123456789';
preg_match('/[0-9]+/',$string1,$matches);
print_r($matches[0]);
preg_match('/[0-9]+/',$string2,$matches);
print_r($matches[0]);
?>

Categories