preg_replace help

Discussion in 'PHP' started by adbox, Feb 7, 2010.

  1. #1
    Hello all,
    I want to remove all characters except the ones in my preg_replace function.

    Here is what I have so far:
    
    $allowed = "/[^a-z0-9\\040\\.\\-\\_\\\\]/i";
    $v = preg_replace($allowed,"",$title);	
    
    Code (markup):
    but I also want to include all instances of % characters and $ characters

    how should I add these?

    Thanks a ton!
    adbox
     
    adbox, Feb 7, 2010 IP
  2. happpy

    happpy Well-Known Member

    Messages:
    926
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    120
    #2
    by % chars you mean html-entities?

    by $ chars you mean set variables?
     
    happpy, Feb 7, 2010 IP
  3. adbox

    adbox Well-Known Member

    Messages:
    906
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    155
    Digital Goods:
    1
    #3
    no no

    I just would like to leave those characters in the string, instead of removing thing with the preg_replace function. So how can I add them to the allow list already in the preg_replace function?
     
    adbox, Feb 7, 2010 IP
  4. Djkanna

    Djkanna Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Sorry I'm not a RegExp person but you could try this :)

    $allowed = "/[^a-z0-9\\040\\.\\-\\_\\\\\][$][%]/i";
    Code (markup):
     
    Djkanna, Feb 8, 2010 IP
  5. Djkanna

    Djkanna Peon

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I've only just realised that you escape by using backslash so you could add to your string \$ and \% I think I'm not actually sure but you could try that too :)
     
    Djkanna, Feb 9, 2010 IP
  6. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #6
    $allowed = "/[^a-z0-9\$%\\040\\.\\-\\_\\\\]/i";
    $v = preg_replace($allowed,"",$title);
    PHP:
     
    danx10, Feb 9, 2010 IP