A little pointer please?

Discussion in 'PHP' started by brendgard, Jul 29, 2010.

  1. #1
    Maybe I'm not looking the right way, but I'm not finding what I need through search so could I get a bit of help please?

    I have a line of code that replaces anything that is not a letter or number with "-"

    
    
    $text = ereg_replace('[^A-Za-z0-9]', '-', $text);
    
    
    
    Code (markup):
    But I also need to add in the "~" character to the list of ones not replaced. How do I do that? I tried these

    
    
    $text = ereg_replace('[^A-Za-z0-9~]', '-', $text);
    $text = ereg_replace('[^A-Za-z0-9\~]', '-', $text);
    $text = ereg_replace('[^A-Za-z0-9/~]', '-', $text);
    
    
    
    Code (markup):
    But every one of them still replaced the "~" with a "-". So obviously I'm missing something, probably pretty basic knowing me lol.

    Thanks!
     
    brendgard, Jul 29, 2010 IP
  2. po_taka

    po_taka Peon

    Messages:
    14
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    echo preg_replace('|[^0-9a-zA-Z~]|', '-','"0889-507-asd~222##');
    PHP:
     
    po_taka, Jul 29, 2010 IP
    Deacalion likes this.
  3. brendgard

    brendgard Active Member

    Messages:
    590
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    58
    #3
    
    
    $text = ereg_replace('|[^A-Za-z0-9~]|', '-', $text);
    
    
    
    
    Code (markup):
    Just returns a whole bunch of errors to me. Any other ideas?

    Thanks!
     
    brendgard, Jul 29, 2010 IP
  4. po_taka

    po_taka Peon

    Messages:
    14
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    use preg_replace
     
    po_taka, Jul 29, 2010 IP
  5. brendgard

    brendgard Active Member

    Messages:
    590
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Hmmmm

    
    
    $text = preg_replace('|[^A-Za-z0-9~]|', '-', $text);
    
    
    
    Code (markup):
    goes back to replacing the "~" with a "-".

    Also tried

    
    
    $text = preg_replace('[^A-Za-z0-9~]', '-', $text);
    $text = preg_replace('[^A-Za-z0-9\~]', '-', $text);
    $text = preg_replace('[^A-Za-z0-9/~]', '-', $text);
    $text = preg_replace('|[^A-Za-z0-9\~]|', '-', $text);
    $text = preg_replace('|[^A-Za-z0-9/~]|', '-', $text);
    
    
    
    Code (markup):
    and they do too. Any other suggestions? Thanks for the help!
     
    brendgard, Jul 29, 2010 IP
  6. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #6
    $text = preg_replace('~[^\w]~', '~', $text);
    PHP:
     
    danx10, Jul 29, 2010 IP
  7. brendgard

    brendgard Active Member

    Messages:
    590
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    58
    #7
    I gave it a try danx10, it does the same thing. Guess I'm gonna be stuck with it the way it is. Thanks anyways everybody.
     
    brendgard, Jul 30, 2010 IP
  8. ProxyGod

    ProxyGod Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I can't stand Regex either. It looks perfect but still doesn't work.
     
    ProxyGod, Jul 31, 2010 IP
  9. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #9
    Mine should work as it would replace all a-zA-Z0-9 with - (and NOT replace the ~ character)

    Which is what your description suggests you wanted?
     
    danx10, Jul 31, 2010 IP
  10. brendgard

    brendgard Active Member

    Messages:
    590
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    58
    #10
    Ummmm not quite lol. Sorry if I didn't explain it clearer. I need to replace any character that is NOT in the set (a-z, A-Z, 0-9, ~) with a "-". Right now it replaces anything that is not in the set (a-z, A-Z, 0-9) but I'm looking to add "~" to that set of characters not being replaced.

    For example, if you feed "~All cats are furry-usually~" into it, right now it spits out "-All-cats-are-furry-usually-" and I'm looking to make it spit out "~All-cats-are-furry-usually~" instead.

    Thanks!
     
    brendgard, Aug 2, 2010 IP
  11. XoC

    XoC Active Member

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #11
    Hey, providing I actually understood what you're after (seems straight forward) this will do it!

    $content = preg_replace('/[^a-zA-Z0-9~]/', '-', $content);
    Code (markup):
    Let me know if that helps ya!
     
    XoC, Aug 2, 2010 IP
  12. brendgard

    brendgard Active Member

    Messages:
    590
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    58
    #12
    Yea, that did it. Thanks!
     
    brendgard, Aug 3, 2010 IP