$re = '/.{1,5000}(?:\.|;)/s';
$str = 'I need to modify the following script so that it says the following slogan
"Select 5000 characters (or, failing that, no more than 5000) from the string entered in the variable (Var), but stop at the last full stop or semicolon you find" (so, maybe select 4912 characters, for example)
In summary
up to 5000 (not beyond 5000)
but the limit to be selected is a full stop or a semicolon.
I think this should be done with a Regular Expression, but I don\'t know how I could do it;
Thank you very much in advance
';
preg_match($re, $str, $matches, PREG_OFFSET_CAPTURE, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php