PHP - How to remove pagination page variable from URL? - php

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

Related

How to edit permalink with str_replace() Function

I'm trying to edit a wordpress permalink like the one here...
echo get_permalink();
This permalink as it is will output...
domain.com/directory/mycustompage/
I'm looking for a way using str_replace() to make the the URL to become...
domain.com/NEWDIRECTORY/?draft=mycustompage
So as you can see I want to change the middle directory to "NEWDIRECTORY" and also grab the last directory name "mycustompage" and use that as the parameter.
I apologise if this seems easy, I've just started seriously coding with PHP this year at school.
Thanks
This should do what you're after. I've put the URL in a variable and then exploded it and rebuilt it.
<?php
$url = "domain.com/directory/mycustompage/";
$exploded = explode('/', $url);
$newURL = $exploded[0].'/NEWDIRECTORY/?draft='.$exploded[2];
echo $newURL;
?>
Outputs domain.com/NEWDIRECTORY/?draft=mycustompage as requested
Here's what I have and works! thanks for pointing me in the right direction!
$prev = get_permalink($prevID);
$prev = explode('/', $prev);
$prev = array_filter($prev);
$prev = array_merge($prev, array());
$prev = preg_replace('/\?.*/', '', $prev);
$prev3 = $prev[3];
I then reconstruct the URL.

Remove characters from end of URL

Say I have a URL with something like this:
http://website.com/website/webpage/?message=newexpense
I have the following code to try and get the the URL before the question mark:
$post_url = $actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$link_before_question_mark = explode('?', $actual_link);
$add_income_url = $link_before_question_mark[0];
In this example I would get the following URL:
http://website.com/website/webpage/
I'd like to remove the webpage portion of this so the URL is:
http://website.com/website/
How can I do this?
Use parse_url This way you have all components.
$url = 'http://website.com/website/webpage/?message=newexpense';
$pUrl = parse_url( $url);
echo $pUrl['scheme'] . '://' . $pUrl['host'] . $pUrl['path'];
You can do a similar trick using explode. Then pop the parts you don't need and implode the url back together. If you are sure that the part after '?' never contains a '/', you can replace your code with this one. If you're not sure, you should first remove the part after '/' and then run this code to remove the last part of the path.
<?php
$url = 'http://website.com/website/webpage/?message=newexpense';
$parts = explode('/', $url);
// Remove the last part from the array
$lastpart = array_pop($parts);
// If the last part is empty, or the last part starts with a '?'
// this means there was a '/' at the end of the url, so we
// need to pop another part.
if ($lastpart == '' or substr($lastpart, 0, 1) == '?')
array_pop($parts);
$url = implode('/', $parts);
var_dump($url);
I'd probably use dirname; it's specifically designed to strip the last stuff after a "/"...
$url = "http://website.com/website/webpage/?message=newexpense";
echo dirname(dirname($url))."/"; // "http://website.com/website/"
(As it says in the documentation, "dirname() operates naively on the input string, and is not aware of the actual filesystem...", so it's quite safe to use for this kind of purpose.)
Try it with explode
<?php
$actual_link = "http://website.com/website/webpage/?message=newexpense]";
$link_before_question_mark = explode('?', $actual_link);
$add_income_url = $link_before_question_mark[0];
$split=explode('/', $add_income_url);
echo $split[0]."//".$split[2]."/".$split[3]."/";
?>
Even better is...
<?php
$actual_link = "http://website.com/website/webpage/?message=newexpense]";
$split=explode('/', $actual_link);
echo $split[0]."//".$split[2]."/".$split[3]."/";
?>

Remove character from php variable

I am trying to remove first 51 character of a long URL , I'm using
$sql = $db->Query(some query);
$mysite = $db->FetchArray($sql);
$str = $mysite['url'] ;
$str2 = substr('$str',51);
Above code return blank value, it works fine if i use plan text for $str
e.g.
$str = "I am looking for a way to pull the first 100 characters from a string"
$str2 = substr('$str',10);
my url is like "https://dl.dropbox.com/u/55299544/google.html?urlc=http://example.com"
i want to get http://example.com from database to show on user page,
how can i do this?
You're making this too complicated. If you are trying to get the http://example.com from the long URL then do this.
<?php
sql = $db->Query(some query);
$mysite = $db->FetchArray($sql);
$str = $mysite['url'] ;
$query = parse_url($str, PHP_URL_QUERY );
parse_str($query, $link);
echo $link["urlc"]; //http://example.com
?>

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.

Categories