SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    Non-Member
    Join Date
    Aug 2005
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Please Help - Modifying a string so it can go in the url

    sorted thanks
    Last edited by themainman; Dec 22, 2005 at 12:20.

  2. #2
    Sell crazy someplace else markl999's Avatar
    Join Date
    Aug 2003
    Location
    Manchester, UK
    Posts
    4,007
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could just use rawurlencode and rawurldecode

  3. #3
    Non-Member
    Join Date
    Aug 2005
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorted
    Last edited by themainman; Dec 22, 2005 at 12:21.

  4. #4
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $string preg_replace('/[^A-Za-z0-9_-]/i'''$string); 
    That'll kill all characters that aren't in the brackets. Should do what you want

  5. #5
    Non-Member
    Join Date
    Aug 2005
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorted
    Last edited by themainman; Dec 22, 2005 at 12:21.

  6. #6
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No it won't. It'll turn the string:

    "test" lala *' foobar

    Into:

    testlalafoobar

  7. #7
    Non-Member
    Join Date
    Aug 2005
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    sorted
    Last edited by themainman; Dec 22, 2005 at 12:21.

  8. #8
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    PHP Code:
     $string preg_replace('/[^A-Za-z0-9_-]/i'''preg_replace('/[\s]+/i, '_', $string)); 

  9. #9
    SitePoint Guru aamonkey's Avatar
    Join Date
    Sep 2004
    Location
    kansas
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this will do
    _test_lala_foobar
    for that particular string
    PHP Code:
      $new_str preg_replace("/[^\w_-]+/"'_'$str); 
    if you then wanted to remove any leading underscores you could do:
    PHP Code:
      $new_str preg_replace("/^_?/"''$new_str); 

  10. #10
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It'll also replace any other foreign chars with _ which I don't think you'd want if the string was:

    "Should MS invest $$$$'s in it's new ventures"

    Outputs:

    _Should_MS_invest______s_in_it_s_new_ventures_

  11. #11
    Non-Member
    Join Date
    Aug 2005
    Posts
    56
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok thanks for all the help, ill see what works


Bookmarks

Posting Permissions

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