简体   繁体   中英

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 industr y. L orem 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 boo k. I t has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchang ed. I t 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?

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

Use preg_split('/\\.[\\s|$]/', $input_line); .

That will split on dot and either space or new line.

https://www.phpliveregex.com/p/qiC
https://3v4l.org/FOaHu

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM