Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    String operations

    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.

  • #2
    New Coder
    Join Date
    Dec 2009
    Posts
    84
    Thanks
    6
    Thanked 3 Times in 3 Posts
    You want to remove page=2 or page=2& ?

    May you can fix it using str_replace
    E.g.
    PHP Code:
    $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&
    Last edited by sir.jones; 01-14-2010 at 10:26 AM.
    Best Regards,

  • #3
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 ?

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,866
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Let me ask, from where you get that string? Can't you just edit your source to get the desired result?
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #5
    New to the CF scene
    Join Date
    Jan 2010
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am writing code for pagination.

  • #6
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,866
    Thanks
    160
    Thanked 2,224 Times in 2,211 Posts
    Quote Originally Posted by qazzaq View Post
    I am writing code for pagination.
    Why do you need to do it then? I think, there's something wrong with your approach.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #7
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    PHP Code:
    $string "page=2&param1=value1&param2=value2";
    parse_str($string,$temp);
    if(isset(
    $temp['page'])) unset($temp['page']);
    $string http_build_query($temp); 
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • #8
    New Coder
    Join Date
    Dec 2009
    Posts
    84
    Thanks
    6
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by qazzaq View Post
    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 , 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), "$");
    Best Regards,

  • #9
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by sir.jones View Post
    in your 1st you don't said before , 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:
    PHP Code:
    // 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:
    Code:
    ... &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.
    Last edited by kbluhm; 01-14-2010 at 02:19 PM.

  • #10
    New Coder
    Join Date
    Dec 2009
    Posts
    84
    Thanks
    6
    Thanked 3 Times in 3 Posts
    i agree using http_build_query, because it using for pagination / url and also already posted at: http://www.php.net/manual/en/functio...uild-query.php
    Best Regards,


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •