Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

regex PHP

  • 04-03-2013 01: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, Registered Users 2 Posts: 1,929 ✭✭✭PrzemoF




  • Moderators, Science, Health & Environment Moderators, Social & Fun Moderators, Society & Culture Moderators Posts: 60,119 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,144 ✭✭✭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