freebase - PHP display sentences up to 100 characters -
my php script calls freebase api , outputs paragraph little bit of regex , other parsing magic on , return data variable $paragraph
. paragraph made of multiple sentences. want return shorter version of paragraph instead.
i want display first sentence. if less 100 characters i'd display next sentence until @ least 100 characters.
how can this?
you don't need regular expression this. can use strpos()
offset of 99 find first period @ or after position 100 - , substr()
grab length.
$shortpara = substr($paragraph, 0, strpos($paragraph, '.', 99) + 1);
you want add bit of checking in case original paragraph less 100 characters, or doesn't end period:
// find first period @ character 100 or greater $breakat = strpos($paragraph, '.', 99); if ($breakat === false) { // no period @ or after character 100 - use whole paragraph $shortpara = $paragraph; } else { // take , including period found $shortpara = substr($paragraph, 0, $breakat + 1); }
Comments
Post a Comment