Alex Alex - 5 months ago 14
HTML Question

Split text in half, but at the nearest sentence

Example of a $text variable:


Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do
eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad
minim veniam, quis nostrud exercitation ullamco laboris nisi ut
aliquip ex ea commodo consequat. Duis aute irure dolor in
reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
culpa qui officia deserunt mollit anim id est laborum.


Splitting it in half:

$half = strlen($text) / 2;


will get me to the "o" character in "consequat".

How can I find the position of the nearest sentence delimiter (dot) to the half of the text? In this example it's 7 characters after that "o".

Also this text contains HTML code.
I want to ignore the HTML when finding out the half-point of the text, and ignore dots from within html attributes etc.

Answer

Take a look at substr, strip_tags and strpos. With the help of strpos you find the position of the next dot and with strip_tags you strip all the html tags from the string.

$string = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborumt.';
$string = strip_tags($string);
$half = intval(strlen($string)/2);
echo substr($string, 0, strpos($string, '.', $half)+1);

Consider that you have to make sure a dot exists after the value of $half or else the output is not going to be what you desire.

Perhaps something like this?

if (strpos($string, '.', $half) !== false)
    echo substr($string, 0, strpos($string, '.', $half)+1);
else
    echo substr($string, 0, $half) . '...';
Comments