Split the data in php - 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

Related

PHP - How to remove pagination page variable from URL?

I am tryign to remove pagination page ID from URL. For example I have URL looks like this:
$urlVal = "http://192.168.1.233/sitename/property-list?page=13&page=11&sproperty=for sale&srooms=1,10&scity=&scountry=&lat=31.0000000&long=35.0000000&sprice=100,100000";
And want to remove, If any of matches match from URL:
1. page=1
2. page=1&page=2
3. page=1&page=2$page=3
4. page=1&page=2$page=3$page=4
In my current pagination code, Previous page is concating everytime when page is changed thats why I want to remove all page.
I have used this code but not working well.
$urlVal = "http://192.168.1.233/sitename/property-list?page=13&page=11&sproperty=for sale&srooms=1,10&scity=&scountry=&lat=31.0000000&long=35.0000000&sprice=100,100000";
//$getUrl =$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
$parts = parse_url($urlVal);
$urlVar = "";
$urlVar = $parts['query'];
echo $urlVar = preg_replace('/page=[0-9]&+/', '', $urlVar);
Try my code here: http://codepad.org/lSHV7a7w
I hope you understand what I am trying to do.
Thanks.
Try this.
$urlVal = "http://192.168.1.233/sitename/property-list?page=13&page=11&sproperty=for sale&srooms=1,10&scity=&scountry=&lat=31.0000000&long=35.0000000&sprice=100,100000";
$parts = parse_url($urlVal);
parse_str($parts['query'], $query);
unset($query['page']);
$newUrl = "{$parts['scheme']}://{$parts['host']}{$parts['path']}?" . http_build_query($query);
echo $newUrl;
// Regx Pattern
echo preg_replace('/&?page=[0-9]+&?/', '', $parts['query']);
How about using concatenation of numbers as page value?
$urlVal = "http://192.168.1.233/sitename/property-list?page=11,13&etc..."
It shortens url and makes removing parameter easy:
$urlVar = preg_replace('/page=[0-9,]+&/', '', $urlVar);
You can extract them from url using:
preg_match("/page=([0-9,]+)&/", $urlVal, $m);
$pages = explode(",", $m[1]);

php count wordand remove

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;

Get value from URL after the last /

I have looked around for this but can only find links and references to this been done after an anchor hashtag but I need to get the value of the URL after the last / sign.
I have seen this used like this:
www.somesite.com/archive/some-post-or-article/53272
the last bit 53272 is a reference to an affiliate ID..
Thanks in advance folks.
PHPs parse_url (which extracts the path from the URL) combined with basename (which returns the last part) will solve this:
var_dump(basename(parse_url('http://www.somesite.com/archive/some-post-or-article/53272', PHP_URL_PATH)));
string(5) "53272"
You can do this :
$url = 'www.somesite.com/archive/some-post-or-article/53272';
$id = substr(url, strrpos(url, '/') + 1);
You can do it in one line with explode() and array_pop() :
$url = 'www.somesite.com/archive/some-post-or-article/53272';
echo array_pop(explode('/',$url)); //echoes 53272
<?php
$url = "www.somesite.com/archive/some-post-or-article/53272";
$last = end(explode("/",$url));
echo $last;
?>
Use this.
I'm not an expert in PHP, but I would go for using the split function: http://php.net/manual/en/function.split.php
Use it to split a String representation of your URL with the '/' pattern, and it will return you an array of strings. You will be looking for the last element in the array.
This will work!
$url = 'www.somesite.com/archive/some-post-or-article/53272';
$pieces = explode("/", $url);
$id = $pieces[count($pieces)]; //or $id = $pieces[count($pieces) - 1];
If you always have the id on the same place, and the actual link looks something like
http://www.somesite.com/archive/article-post-id/74355
$link = "http://www.somesite.com/archive/article-post-id/74355";
$string = explode('article-post-id/', $link);
$string[1]; // This is your id of the article :)
Hope it helped :)
$info = parse_url($yourUrl);
$result = '';
if( !empty($info['path']) )
{
$result = end(explode('/', $info['path']));
}
return $result;
$url = 'www.somesite.com/archive/some-post-or-article/53272';
$parse = explode('/',$url);
$count = count($parse);
$yourValue = $parse[$count-1];
That's all.

Remove URL regardless of format

Having a brain freeze...
Have a URL which may be in any of the formats :
http://url.com/stuff
url.com/somestuff
www.url.com/otherstuff
https://www.url.com/morestuff
You get the picture.
How do I remove the .com part to leave just the various 'stuff' parts ? For example, the above would end up :
stuff
somestuff
otherstuff
morestuff
You could achieve that using the following code:
$com_pos = strpos($url, '.com/');
$stuff_part = substr($url, $com_pos + 5);
Click here to see the working code.
This should do the trick for you!
<?php
$url = "http://url.com/stuff";
$querystring = preg_replace('#^(https|http)?(://)?(www.)?([a-zA-Z0-9-]+)\.[a-zA-Z]{2,6}/#', "", $url);
echo $querystring;
I submitted this answer because I'm not very fond of solutions using explode() to handle this. Maybe your query string contains more slashes so, you'd have to write exceptions for those cases.
You can use explode to make an array, then get the last element from the array.
$str = 'http://url.com/stuff';
$arr = explode('/', $str);
echo end($arr); // 'stuff'
$path = parse_url('http://url.com/stuff', PHP_URL_PATH);
If you leave the second parameter unspecified you can return an array including the domain etc.
Use explode function to divide the string.
<?php
$url = "http://url.com/stuff";
$stuff = explode("/", $url);
echo $stuff[sizeof($stuff) - 1];
?>
I used sizeof to access to last element.
preg_replace("/^(https?:\/\/)?[^\/]+/" ,"", $url);

PHP update url in variable

$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)));

Categories