I found this code which will match at most 300 chars, then break at the next nearest word-break:
$var = 'This is a test text 1234567890 test check12.' # 44 chars
preg_match('/^.{0,300}(?:.*?)\b/iu', $var, $matches);
echo $matches[0];
44 is lower than 300, so I expect the output to be the same like $var.
But the output is:
This is a test text 1234567890 test check12 # 43 chars
$matches[0] is not giving me the dot at the end, however $var does. Anyone can tell me how to get the full string (with the dot)?
I could get the expected result by:
\b\bwith$EDIT:
In your pattern the dot at the end of the string is acting as a word boundary, so you are able to match everything before the dot. If you put a
.*after the\b, you’ll see that it will match the dot.See this for more info on how word boundaries in regex work.