...

View Full Version : String operations



qazzaq
01-14-2010, 06:49 AM
Hello All,

There is a string

"page=2&param1=value1&param2=value2" or
"param1=value1&page=2&param2=value2".

I want to remove page=2 so that string become
"param1=value1&param2=value2" or
"param1=value1&param2=value2".

Is there in optimize code present to solve this ?

Thanks in advance.

sir.jones
01-14-2010, 09:09 AM
You want to remove page=2 or page=2& ?

May you can fix it using str_replace
E.g.

$OldString = "page=2&param1=value1&param2=value2";
$NewString = str_replace(array('page=2&'), '', $OldString);
echo $NewString; //test using array if you are playing with array before or just delete it

if there as url link you can use
mod_rewrite
but first enable LoadModule rewrite_module modules/mod_rewrite.so in httpd.conf file in dir. apache/conf and delete #

then open/put to .httaccess file:
e.g.
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^value1& value1&page=2&

qazzaq
01-14-2010, 09:24 AM
Thanks for the reply.

But in case of page=2, 2 is any natural no. i.e. (0 to 32000).

In this case what can we do ?

abduraooft
01-14-2010, 09:39 AM
Let me ask, from where you get that string? Can't you just edit your source to get the desired result?

qazzaq
01-14-2010, 09:48 AM
I am writing code for pagination.

abduraooft
01-14-2010, 10:00 AM
I am writing code for pagination.:confused: Why do you need to do it then? I think, there's something wrong with your approach.

JAY6390
01-14-2010, 12:11 PM
$string = "page=2&param1=value1&param2=value2";
parse_str($string,$temp);
if(isset($temp['page'])) unset($temp['page']);
$string = http_build_query($temp);

sir.jones
01-14-2010, 12:15 PM
Thanks for the reply.

But in case of page=2, 2 is any natural no. i.e. (0 to 32000).

In this case what can we do ?

in your 1st you don't said before :mad:, so i just shoot the target

use this will be work fine:

$OldString = "page=2&param1=value1&param2=value2";
$OldString = trim(preg_replace("/\bpage=\d+&?/", "", $OldString), "$");

kbluhm
01-14-2010, 01:02 PM
in your 1st you don't said before :mad:, so i just shoot the target

use this will be work fine:

$OldString = "page=2&param1=value1&param2=value2";
$OldString = trim(preg_replace("/\bpage=\d+&?/", "", $OldString), "$");

JAY6390 answered with my suggestion. What if the visitor has manually typed in a page that is not a number? I think it's best to parse the query string, explicitly remove the key in question, and rebuild it. This way you're removing the key regardless of the value. It also ensures you're working with a properly formatted query string.

Conversely, if you want to add page to the query string:


// THIS IS GOOD
parse_str( $_SERVER['QUERY_STRING'], $query );
$query['page'] = 2;
$query = http_build_query( $query );

// THIS IS BAD
$query = $_SERVER['QUERY_STRING'];
$query .= '&page=2';


The first is good because you're overwriting the array key, regardless of its existence.

The second is bad because you could potentially get a crapped out query string if page already existed... and if you're building the query string into pagination links, over a number of page views it could begin to grow:


... &page=2&page=3&page=4&page=5 ...

The query string would still be accepted properly, as the last page key would be the one used. But it would look horrible, possibly break if the string grew past the browser's address limitations, and could potentially wreak havoc on your SEO.

sir.jones
01-14-2010, 03:03 PM
i agree using http_build_query, because it using for pagination / url and also already posted at: http://www.php.net/manual/en/function.http-build-query.php



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum