简体   繁体   中英

How to select only first few words not lines of sentences?

I am using the following snippet:

 implode(' ', array_slice(explode(' ', $sentence), 0, 10));

The problem is that if there are two or 3 lines than it repeats the function and shows 10 words or etc per line.

How can i do it so it only selects the first 10 words from the first sentence and not repeat itself.

$lines = split("[\n|\r]", $sentence);
implode(' ', array_slice(explode(' ', $lines[0]), 0, 10));

First split into many lines, then do your action on only the first line.

Make sure that $sentence only contains the first line of whatever you're feeding it, before it ever gets to this line of code.

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