how to remove some word on url inp php?
URL : /hiaz/0.1.1/index.php?route=product/product&product_id=153&tracking=507b790fadc48
i just want :
tracking=507b790fadc48
If you have the full url stored in the var $myurl:
$result = substr($myurl, strrpos($myurl, "tracking"), strlen ($myurl) );
You can make use of $_GET if you are working in the index.php. Alternatively, if you wish to grab whole query string, you can use $_SERVER['QUERY_STRING']
What you have is
$url = /hiaz/0.1.1/index.php?route=product/product&product_id=153&tracking=507b790fadc48
What you want is
$newurl = /hiaz/0.1.1/index.php?tracking=507b790fadc48
First extract part of url upto "?" character.
Then extract the tracking part and combine.
$part1 = substr($url,0,strpos($url,'?'));
$part2 = substr($url,strpos($url,"tracking"),strlen($url)-strpos($url,"tracking"));
$newurl = $part1 + $part2;
Related
I want to explode urls to get the host.
$link = str_replace("www.", "", $_POST['link']);
$part1 = explode("//", $link);
$part2 = explode(".", $part1[1]);
$host = $part2[0];
So if the $_POST['link'] is e.g.
https://www.youtube.com/watch?v=udm5jUA-2bs
I want to get just youtube.
In this first way I get explode() expects parameter 2 to be string, array given" error message.
With this method:
$host = var_dump(parse_url($_POST['link'], PHP_URL_HOST));
I get www.youtube.com but I want only youtube.
Extract the url domain using parse_url() function with mode PHP_URL_HOST.
We will use explode() function to break it into parts using . as delimiter.
Now, the URL can be of two types, either youtube.com, or www.youtube.com, or %.youtube.com
We count the parts, and if they are two, we use the first value, else the second value.
DEMO: https://3v4l.org/KlErW
$url_host_parts = explode('.', parse_url($_POST['link'], PHP_URL_HOST));
$host = (count($url_host_parts) == 2) ? $url_host_parts[0] : $url_host_parts[1];
what about this without any regex? You can use parse_url() and explode() to get what you want i.e youtube
<?php
$url = 'https://www.youtube.com/watch?v=udm5jUA-2bs';
$parse = parse_url($url);
//print_r($parse);
echo explode('.',$parse['host'])[1];
?>
Output:
youtube
DEMO: https://3v4l.org/kG1nD
You can use regex in preg_match() to do this work.
$url = "https://www.youtube.com/watch?v=udm5jUA-2bs";
preg_match("/(\w+)(?=\.\w{2,}(\/|\?|$))/", $url, $matches);
echo $matches[1]; // youtube
Check result in demo
Well sorry for the probably misleading title. Wasn't sure how to describe it better.
When accessing the status page I want to get the attached ID. But I don't want to use GET fields (wordpress makes /status?id=2134 to /status/?id=1234 - that's the only reason actually).
So this is my url
http://foo.bar.com/status/1234/
I want to get 1234
Okay fine. I could use something like $_SERVER["REQUEST_URI"] + trim() for example. Probably regex would be the key to get this job done since one could do something like /status/1234/foo/bar/baz/.. But I'm wondering if there is something builtin with PHP to get this part of the url.
Use the parse_url() function, and extract it:
$url = 'http://foo.bar.com/status/1234/';
$path = trim(parse_url($url, PHP_URL_PATH), '/');
$items = explode('/', $path);
$num = array_pop($items);
var_dump($num);
You can also use a regular expression, if that tickles your fancy:
$url = 'http://foo.bar.com/status/1234/';
$path = parse_url($url, PHP_URL_PATH);
preg_match('~/status/(?P<num>\d+)/?~', $path, $result);
$num = isset($result['num']) ? $result['num'] : null;
var_dump($num);
Try to parses a URL and returns an associative array containing any of the various components of the URL that are present using parse_url, explode it using explode and finally select status id using end
Try like this
$url = 'http://foo.bar.com/status/1234/';
$statusId = explode('/',trim(parse_url($url, PHP_URL_PATH), '/'));
print end($statusId);
Demo Ex http://ideone.com/34iDnh
trim- http://php.net/trim
explode-http://php.net/explode
parse_url-[1]: http://php.net/manual/en/function.parse-url.php
I have a variabe named $link in PHP that repeats itself and it has data like:
http://www.ankinfos.com/our-portfolio/www.abc.com
http://www.ankinfos.com/our-portfolio/www.cab.com
http://www.ankinfos.com/our-portfolio/www.zzz.com
.
.
.
.
http://www.ankinfos.com/our-portfolio/www.lal.com
I want to remove the text or data before www.abc.com so that it becomes
http://www.ankinfos.com/our-portfolio/www.abc.com
to
www.abc.com
and
http://www.ankinfos.com/our-portfolio/www.abc.com
to
www.cab.com
and so on
Please let me know any solution for that in jquery or php
I think the easiest way in php is to do
$url = "http://.../www.abc.com";
$url = explode('/', $url);
$yourUrl = $url[count($url) - 1];
As for javascript:
var url = "http://.../www.abc.com";
url = url.split("/");
var yourUrl = url[url.length - 1];
http://php.net/manual/en/function.explode.php
$myArray = explode("http://www.ankinfos.com/our-portfolio/", $link);
array_shift($myArray);
print_r($myArray);
This will explode each url (Which I assume is constant) and will remove it's first element (since the delimiter is at the start, it will cause an empty string to be placed as the first item).
Now, you can iterate the items.
Use split() function in javascript
var fullLink = "http://www.ankinfos.com/our-portfolio/www.abc.com";
var url = fullLink.split('/');
url = url[url.length-1]
alert(url)
http://jsfiddle.net/Gsruv/
And explode() in PHP
I have url in the following types
http://domain.com/1/index.php
http://domain.com/2/index.php
http://domain.com/3/index.php
http://domain.com/4/index.php
I need to retrieve only the number from the url .
for example,
when i visit http://domain.com/1/index.php , It must return 1.
Have a look at parse_url.
$url = parse_url('http://domain.com/1/index.php');
EDIT: Take a look at $_SERVER['REQUEST_URI'], to get the current URL. Use that instead of $url['path'].
Then you can split $url['path'] on /, and get the 1st element.
// use trim to remove the starting slash in 'path'
$path = explode('/', trim($url['path'], '/'));
$id = $path[0]; // 1
Given the information provided, this will do what you ask... it's not what I would call a robust solution:
$url = $_SERVER['PATH_INFO']; // e.g.: "http://domain.com/1/index.php";
$pieces = explode("/", $url);
$num = $pieces[3];
split the server path by forward slashes (explode('/', $_SERVER['REQUEST_PATH']);)
remove empty entry from the beginning
take the first element
make sure it is an integer (intval(), or a simple (int) cast).
No need to use regular expressions for this.
You use preg_match() to match the domain and get the 1st segment.
$domain = http://www.domain.com/1/test.html
preg_match("/http:\/\/.*\/(.*)\/.*/", "http://www.domain.com/1/test.html");
echo $matches[1]; // returns the number 1 in this example.
$link = http://site.com/view/page.php?id=50&reviews=show
How can we add &extra=video after id=50?
id is always numeric.
url can have many other variables after ?id=50
&extra=video should be added before the first & and after the 50 (value of id)
It will be used this way:
echo 'Get video';
Thanks.
As Treffynnon says, the order seldomly matters. However, if you really need if for some reason, just use
parse_url to get the querystring
parse_str to create an array of parameter
array_splice to inject a parameter
http_build_query to rebuild a proper query string
This will do it for you
<?php
$linkArray = explode('&',$link);
$linkArray[0] += '&extra=video';
$link = implode('&',$linkArray);
?>
Explode will split the link string at every &, so it doesn't care how many elements you have in the url.
The first element, will be everything including the id=## before the first & sign. So we append whatever you want to appear after it.
We put our array together again as a string, separating each element by an &.
Is ID always the first post parameter? If so, then you could jsut do some sort of string manipulation. Use strpos($link, "&") to find out the position where you want to insert. Then do a few substr() based on that position and then append them all together. Its kind of hacky I know, but it will definitely work.
$pos = strpos($link, "&");
$first = substr($link, 0, $pos);
$last = substr($link, $pos);
$extra = "&extra=video";
$newLink = $first . $extra . $last;
See this link for some of the string manipulation functions that I mentioned above: http://us3.php.net/strings
i would suggest to use functions specifically aimed at url parsing, not general string functions:
$link = 'http://site.com/view/?id=50&reviews=show';
$query = array();
parse_str(parse_url($link, PHP_URL_QUERY), $query);
$query['extra'] = 'video';
$linkNew = http_build_url($link, array('query' => http_build_query($query)));