Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

regex PHP

Options
  • 04-03-2013 1:30am
    #1
    Closed Accounts Posts: 2,828 ✭✭✭


    I can't get my head around regex can anyone suggest a regex pattern that would remove hyphens, spaces and apostrophes in a string? I am using the preg_replace function I know I can do it with str_replace but I want to try to get the code on one line.


Comments

  • Closed Accounts Posts: 249 ✭✭OneIdea


    justryan wrote: »
    I can't get my head around regex can anyone suggest a regex pattern that would remove hyphens, spaces and apostrophes in a string? I am using the preg_replace function I know I can do it with str_replace but I want to try to get the code on one line.

    [PHP]
    $str = " Don't try - this at home";

    $str = preg_replace("#\'|\s+|-#", "", $str);

    echo $str; // Donttrythisathome
    [/PHP]


  • Closed Accounts Posts: 2,828 ✭✭✭Reamer Fanny


    OneIdea wrote: »
    [PHP]
    $str = " Don't try - this at home";

    $str = preg_replace("#\'|\s+|-#", "", $str);

    echo $str; // Donttrythisathome
    [/PHP]

    Genius! It worked :D


  • Registered Users Posts: 1,931 ✭✭✭PrzemoF




  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,092 Mod ✭✭✭✭Tar.Aldarion


    Never really did php, why are there #'s at the start and end and why is there a|, for or? Why is there no \ before -?


  • Closed Accounts Posts: 249 ✭✭OneIdea


    Never really did php, why are there #'s at the start and end and why is there a|, for or? Why is there no \ before -?

    Some notes about it here http://php.net/manual/en/regexp.reference.delimiters.php

    Other then that, we could have used #'|\s+|-# and not escape the apostrophe, its just a habit of mine...

    s+ is the regex for spaces, but we need to escape it or it would just match the letter s


  • Advertisement
  • Closed Accounts Posts: 7,145 ✭✭✭DonkeyStyle \o/


    justryan wrote: »
    I know I can do it with str_replace but I want to try to get the code on one line.
    You can do it in one line with str_replace, just pass in an array.
    str_replace(array('something', 'something else'), '', $string)
    Still worth learning regex though, you can take it with you to other languages and situations.


  • Closed Accounts Posts: 2,828 ✭✭✭Reamer Fanny


    You can do it in one line with str_replace, just pass in an array.
    str_replace(array('something', 'something else'), '', $string)
    Still worth learning regex though, you can take it with you to other languages and situations.

    Yep either would work but preg_replace is certainly more elegant


Advertisement