Skip to content

Instantly share code, notes, and snippets.

@jongacnik
Created March 22, 2018 23:54
Show Gist options
  • Save jongacnik/7477b34926699352cfcb49376aa1736d to your computer and use it in GitHub Desktop.
Save jongacnik/7477b34926699352cfcb49376aa1736d to your computer and use it in GitHub Desktop.
Extract first sentence from text
<?php
function firstsentence($string) {
$parts = preg_split('/(^.*?[a-zA-Z0-9\)\”\"]{2,}[.!?])[\s\”\"]+\W*[A-Z]/', strip_tags($string), -1, PREG_SPLIT_DELIM_CAPTURE);
$parts = array_values(array_filter($parts));
return (is_array($parts) && isset($parts[0])) ? $parts[0] : $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment