phpregexsplitcpu-wordapostrophe

Split a string into words, preserving contractions but removing leading apostrophes from words


I would like to split a string where any character is a space or punctuation (excluding apostrophes). The following regex works as intended.

/[^a-z']/i

Words like I'll and Didn't are accepted, which is great.

The problem is with words like 'ere and 'im. I would like to remove the beginning apostrophe and have the words im and ere.

I would ideally like to stop/remove this within the regex pattern if possible.


Solution

  • Try this

    $str = "Words like I'll and Didn't are accepted, which is great. 
            The problem is with words like 'ere and 'im";
    print_r(preg_split("/'?[^a-z']+'?/i", $str));
    //Array ( [0] => Words [1] => like [2] => I'll [3] => and [4] => Didn't ... 
    //        [16] => ere [17] => and [18] => im )