php - How to find all occurrences of groups of single letters in a string -


now days people post on forums or other place on web seem love add space every letter random word. know there name tire can't remember. see on post titles.

so have project use regular expression find these group of single letters try concatenate them , put them word.

now have of project done stuck regular expressions because don't it. okay of basics of programming , logic regular expressions still don't them.

so if can me following regex pattern appreciated.

i want following when submits text. group of 3 or more single letters separated space , between words

$string = "lorem p s u m dolor sit m e t, consectetur adipiscing e l i.";  // know thats far got pattern $pattern = "/\s([a-za-z{1}])\s\w+/";  preg_match_all($pattern, $string, $matches); 

so code above should output:

array  (    [0] => p s u m    [1] => m e t ) 

thanks in advance.

you may use following regular expression:

\b\p{l}(?:\h+\p{l}){2,}\b(?![\s\p{p}]*$) 

see regex demo

it match @ least 3 letters separated space if not @ end of "sentence" (not matched if followed whitespace punctuation @ end of string).

ideone code demo:

$re = '~\b\p{l}(?:\h+\p{l}){2,}\b(?![\s\p{p}]*$)~u';  $str = "lorem p s u m dolor sit m e t, consectetur adipiscing e l i."; preg_match_all($re, $str, $matches); print_r($matches[0]); 

the regex matches:

  • \b - leading word boundary
  • \p{l} - 1 letter
  • (?:\h+\p{l}){2,} - 2 or more sequences of 1 or more horizontal whitespace (\h+) followed letter
  • \b - trailing word boundary
  • (?![\s\p{p}]*$) - match if not followed 0 or more whitespace or punctuation before end of string.

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -