PHP Regex to find first n characters and finish it until end of sentence -
i new in php , sorry if answered here, searched many posts unsuccessful hence asking.
i have large text block , wants output such should return first 250 characters , finish till end of sentence.
$output= preg_replace('/([^?!.]*.).*/', '\\1', substr($string, 250));
can please me in right direction? thanks.
this works:
$output = preg_replace("/^(.{250})([^\.]*\.)(.*)$/", "\\1\\2", $text);
the regex has 3 parts:
^ # beginning of string (.{250}) # 250 characters of ([^\.]*\.) # number of non-periods, followed single period (.*) # $ # end of string
then preg_replace
replaces entire string first 2 parts.
input:
lorem ipsum dolor sit amet, consectetur adipiscing elit. nulla pharetra dignissim mauris, pretium viverra justo tempus at. mauris nisl lectus, accumsan pretium ipsum ac, fringilla vehicula tellus. proin ante mauris, consequat sed mollis id, euismod ac turpis. mauris tellus massa, volutpat sit amet lectus at, imperdiet mollis lacus. praesent dapibus, lacus vel egestas convallis, magna metus pharetra mi, fringilla odio quam eu lacus. nulla congue quam nisi, sed posuere sapien interdum posuere. etiam in nibh felis. sed ac ipsum ut velit dapibus mollis. mauris ut ante ante. pellentesque @ posuere libero, sed posuere risus.
output:
lorem ipsum dolor sit amet, consectetur adipiscing elit. nulla pharetra dignissim mauris, pretium viverra justo tempus at. mauris nisl lectus, accumsan pretium ipsum ac, fringilla vehicula tellus. proin ante mauris, consequat sed mollis id, euismod ac turpis.
Comments
Post a Comment