I am sorting through some lines and some contain email, some don’t.
I need to remove all lines less than 6 characters.
I did a little surfing and found no solid answers so, I tried to write my first expression.
Please tell me if this will work. Did I get it right?
$six-or-more = preg_replace("!\b\w{1,5}\b!", "", $line-in);
Followed by the below which I “stole” which may in fact be superfluous.
$no-empty-lines = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $six-or-more);
$lines = preg_split("/[\s]*[\n][\s]*/", $no-empty-lines);
You can see what I am trying to do but, I think it is a bit much.
Thanks for the tutorial.
\bmatches a “word boundary” — that is, the start or end of a word. It’ll trigger on spaces and punctuation between words as well, so you’ll effectively remove every word between 1 and 5 chars, rather than every line as intended. (BTW, if you have backslashes in strings, you should either be escaping them or using single-quotes instead to avoid future gotchas.)You could try
With the
/mmodifier,^and$match the start and end of each line, respectively, rather than the start and end of the whole string. It matches right before the newline, though, so the line would more than likely become blank rather than getting removed unless you match the newline “after the end” as well.