Search code examples
phpstringexplode

PHP explode string by dot if the next character is a space or an uppercase alphabetic character


I have a paragraph that looks like this:

Lorem Ipsum is simply (not 1.2%) dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s

I want to split this in pharagraph in phrases ended by a dot . but only when that dot is at the end of a phrase, not in the middle (like 1.2%) and when there is an UPPERCASE character after it(and maybe a blank space too). For example if i use:

$arr = explode('.', $paragraph);

it will split that paragraph at each occurrence of that ..

Is there a fast and clean way to obtain that? If yes can somebody please help me understand it?


Solution

  • Use regex to match dot that is before uppercase character or space and use preg_split() to split string based on regex match.

    $arr = preg_split("/\.\s?(?=[A-Z])/", $paragraph);
    

    Check result in demo