Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php - How do I extract capitalised words from a string

Tags:

php

Take this string:

Israel agrees to significant easing of Gaza blockade

I want to return the capitalised words, separated by a comma, like this:

Israel,Gaza

I imagine it must be possible. Any ideas?

like image 763
Steven Avatar asked Feb 04 '26 18:02

Steven


1 Answers

Split the string into it's words with explode(' '), iterate through the words and check if the word is capitalized by checking if it's first letter ($str[0]) is the same as its uppercase variant (strtoupper($str[0])). You can fill an array with the results and then join(',') it

like image 82
Patrick Glandien Avatar answered Feb 07 '26 20:02

Patrick Glandien



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!