I have a site and I would need to get one pages URL with PHP. The URL might be something www.mydomain.com/thestringineed/ or it can www.mydomain.com/thestringineed?data=1 or it can be www.mydomain.com/ss/thestringineed
So it's always the last string but I dont want to get anything after ?
parse_url should help you out.
<?php
$url = "http://www.mydomain.com/thestringineed/";
$parts = parse_url($url);
print_r($parts);
?>
You will use the parse_url function, then look at the path portion of the return.
like this:
$url='www.mydomain.com/thestringineed?data=1';
$components=parse_url($url);
//$mystring= end(explode('/',$components['path']));
// I realized after this answer had sat here for about 3 years that there was
//a mistake in the above line
// It would only give the last directory, so if there were extra directories in the path, it would fail. Here's the solution:
$mystring=str_replace( reset(explode('/',$components['path'])),'',$components['path']); //This is to remove the domain from the beginning of the path.
// In my testing, I found that if the scheme (http://, https://, ...) is present, the path does not include
//the domain. (it's available on it's own as ['host']) In that case it's just
// $mystring=$components['path']);
parse_url() is the function you are looking for. The exact part you want, can be received through PHP_URL_PATH
$url = 'http://php.net/manual/en/function.parse-url.php';
echo parse_url($url, PHP_URL_PATH);
use $_SERVER['REQUEST_URI'] it will return full current page url you can split it with '/' and use the last array index . it will be the last string
You can use:
$strings = explode("/", $urlstring);
Which will remove all the '/' in the url and return an array containing all the words.
$strings[count($strings)-1]
Now has the value of the string you need, but it may contain '?data=1' so we need to remove that:
$strings2 = explode("?", $strings[count($strings)-1]);
$strings2[0]
Has the string you are wanting out of the url.
Hope this Helps!
<?php
$url = 'http://username:password#hostname/path?arg=value#anchor';
print_r(parse_url($url));
echo parse_url($url, PHP_URL_PATH);
?>
and your out put is
Array
(
[scheme] => http
[host] => hostname
[user] => username
[pass] => password
[path] => /path
[query] => arg=value
[fragment] => anchor
)
/path
Related
I have a url - http://example.com/accounts/users/list?showlist=true&id=4 and I need to get the users segment out of this. How can it be done? I know there's substr command, but not quite sure how to use it. Can you please show me an example?
Thanks you.
This should work for you:
(Here I first get the dirname() of the $url, so that the last part is users, then I can grab that with taken the basename() from it)
$url = "http://example.com/accounts/users/list?showlist=true&id=4";
echo basename(dirname($url));
output:
users
If you assume that you always want to get the second value, you first use parse_url to get the path. Then you explode the path by "/" and filter the empty values. Your requested value will be in the second array value.
If you don't expect the value to be always in the second value, please specifiy your question further.
See:
$url = 'http://example.com/accounts/users/list?showlist=true&id=4';
$parts = parse_url($url);
$fragments = explode('/', $parts['path']);
$fragments = array_filter($fragments);
$value = $fragments[2];
echo $value;
Parsing URL with PHP
Explode - one of the usefullnest methods ;)
And it goes this way:
$url = "http://example.com/accounts/users/list?showlist=true&id=4";
$parsed = parse_url($url); // parsing
$parsed['path'] = explode('/', $parsed['path']); // exploding path
print_r($parsed);
This bit of code will provide you with an array looking this way:
[scheme] => http
[host] => example.com
[path] => Array
(
[0] =>
[1] => accounts
[2] => users
[3] => list
)
[query] => showlist=true&id=4
Rest is very similar to JS ;)
echo 'before last: ' . $parsed['path'][count($parsed['path'])-2];
will produce:
before last: users
I want to get username from an url after checking the rest of url is similar to a string.
For eg : I need username from the url 'http://mysite.com/username' . before that need to check
site url as 'http://mysite.com/' and also username part contains only alphabets,numbers,
underscore and periods..
How is it possible using php?
I have tested with PHP5.4.5:
<?php
$simple = 'http://mysite.com/username';
if ( preg_match('/http:\/\/mysite\.com\/(?:([-\w]+)\/?)/', $simple, $match) > 0){
echo 'Username is: '.$match[1] . "\n";
}
$complex = 'http://mysite.com.zzz.yyy/john/';
if ( preg_match('/http:\/\/\w+(?:\.\w+)*\/(?:([-\w]+)\/?)/', $complex, $match) > 0){
echo 'Username is: '.$match[1] . "\n";
}
?>
output:
Username is: username
Username is: john
There are functions for parsing URLs:
http://php.net/manual/en/function.parse-url.php
If you do this:
$url = 'http://mysite.com/username';
$array = parse_url($url);
print_r($array);
You will see this:
Array
(
[scheme] => http
[host] => mysite.com
[path] => /username
)
now you can treat the path of the URL seperately. If there's more to it than just /username/ then you would split on '/' and use the first item returned.
$path_array = explode('/',$array['path']);
This is what you have:
preg_match('/mysite.com//[a-zA-Z0-9-]';, $user_name, $matches);
You have no delimiter, and what's with the ;? Try this:
preg_match('|/mysite.com//[a-zA-Z0-9-]|', $user_name, $matches);
$url = "http://mysite.com/username";
$arr = parse_url($url);
if($arr['host'] != "mysite.com"){
// do something
}
print_r(trim($arr['path'],'/'));
If you want to try this with regex, you can use a pattern with two capture groups, one for the domain name and one for the username, like so:
^http\:\/\/([^/]+)\/(.+)$
(I escaped the colon there because I honestly can't remember off the top of my head whether or not it's necessary. If not, it doesn't need the escape character)
This will look for http:// followed by a string that doesn't contain a / (so it'll keep grabbing until it reaches the next /) and then expects a / and then grabs anything else that follows up until the end of the string.
I'm using this code:
$imageurl = "http://siteadress/sites/default/files/bjorn_4.jpg";
$pieces = explode('/', $imageurl);
print_r($pieces);
to split up an url.
The print_r gives me this result =
Array ( [0] => http://siteadress/sites/default/files/bjorn_4.jpg )
Shouldn't it split up the URL after each /? So it will be Array ( [0] => http:/ [1] => / [2] => siteadresses or something like that?
I think you should try :
$imageurl = [node:field_banner_image];
Because with the quotes explode will think that the string is [node:field_banner_image] and not the string inside.
like Edouard Moinard said
$imageurl = [node:field_banner_image];
$pieces = explode('/', $imageurl);
print_r($pieces);
this should work
Try to save your Array[0] element to any variable and split that variable like:
$image=Array[0];
$pieces = explode('/', $image);
print_r($pieces);
I'm unfamiliar with Drupal but a quick read of the documentation gave me this:
http://api.drupal.org/api/drupal/core!includes!token.inc/function/token_replace/8
token_replace()
Replaces all tokens in a given string with appropriate values.
Hopefully that helps
Just giving a try
Check the view source of the page, check if you are getting %2F or "/"
Secondly, check with explode('/', << the field value >>)
I have the next URL: http://domen.com/aaa/bbb/ccc.
How can I get the string after http://domen.com/?
Thanks a lot.
$sub = substr($string, 0, 10);
But if you actually want to parse the URL (that is, you want it to work with all URLs), use parse_url. For "http://domen.com/aaa/bbb/ccc", it would give you an array like this:
Array
(
[scheme] => http
[host] => domen.com
[user] =>
[pass] =>
[path] => /aaa/bbb/ccc
[query] =>
[fragment] =>
)
You could then compile this into the original url (to get http://domen.com/):
$output = $url['scheme'] . "://" . $url['host'] . $url['path'];
assuming $url contains the parse_url results.
You can use PHP's split.
Your code will be something like:
$s = "http://domen.com/aaa/bbb/ccc";
$vals = split("http://domen.com/", $s);
// $v will contain aaa/bbb/ccc
$v = $vals[1];
parse_url()
http://php.net/manual/en/function.parse-url.php Might be the way to go.
If you simply want the string and the "http://domen.com/" part is fixed:
$url = 'http://domen.com/aaa/bbb/ccc';
$str = str_replace('http://domen.com/','',$url);
Use the regex for example like the function preg_replace
Try this:
preg_replace('/^.*?\w\//', '', $url)
I'm trying to extract an anchor tag and everything behind it from a url using preg_replace. I found one to remove everything after the #, but I want one that removes the # and everything behind it.
http://blah.com#removethis
Thanks,
Steve
You can try the parse_url function:
$url = "http://blah.com#removethis";
print_r(parse_url($url));
fragment - after the hashmark #
Output:
Array
(
[scheme] => http
[host] => blah.com
[fragment] => removethis
)
Another way without regex:
$newurl = substr($url, 0, strpos($url,"#"));
$url = preg_replace('/#.*$/', '', $url);
$url = preg_replace('##.*$#', '', $url);
Don't use regexes when there are proper library functions to do the job.