so i have this string and i want to output all of the words that ends in "ly".
$desi = "DESIDERATA
Go placidly amid the noise and the haste,
and remember what peace there may be in silence.
As far as possible, without surrender,
be on good terms with all persons.
Speak your truth quietly and clearly;
and listen to others,
even to the dull and the ignorant;
they too have their story.
Avoid loud and aggressive persons;
they are vexatious to the spirit.
If you compare yourself with others,
you may become vain or bitter,
for always there will be greater and lesser persons than yourself.
Enjoy your achievements as well as your plans.
Keep interested in your own career, however humble;
it is a real possession in the changing fortunes of time.
Exercise caution in your business affairs,
for the world is full of trickery.
But let this not blind you to what virtue there is;
many persons strive for high ideals,
and everywhere life is full of heroism.
Be yourself. Especially do not feign affection.
Neither be cynical about love,
for in the face of all aridity and disenchantment,
it is as perennial as the grass.
Take kindly the counsel of the years,
gracefully surrendering the things of youth.
Nurture strength of spirit to shield you in sudden misfortune. But do
not distress yourself with dark imaginings. Many fears are born of
fatigue and loneliness.
Beyond a wholesome discipline, be gentle with yourself.
You are a child of the universe no less than the trees and the
stars; you have a right to be here.
And whether or not it is clear to you,
no doubt the universe is unfolding as it should.
Therefore be at peace with God, whatever you conceive Him to be. And
whatever your labors and aspirations,
in the noisy confusion of life, keep peace in your soul.
With all its sham, drudgery, and broken dreams,
it is still a beautiful world.
Be cheerful. Strive to be happy.";
how can I get all the words that ends in "ly" in this string??
Try this solution
$needle = 'ly';
$all = explode(' ', $desi);
$selected = [];
foreach ($all as $key => $value) {
if (substr($value, -strlen($needle)) === (string) $needle) {
$selected[] = $value;
}
}
var_dump($selected);
Here is one solution. First I made an array containing all words:
$words = explode(" ", $desi);
Then test each word and check if it ends with "ly" (using preg_match function)
foreach($words as $word){
if (preg_match("/ly$/", $word)){
echo $word." ";
}
}
I printed out the words, but you can do whatever you like such as save them in an array or ...
NOTE: this code fails to select "clearly" because of the ";" .
This may help you
$desi = "DESIDERATA
Go placidly amid the noise and the haste,
and remember what peace there may be in silence.
As far as possible, without surrender,
be on good terms with all persons.
Speak your truth quietly and clearly;";
$words_array = explode(' ',$desi);
print_r($words_array);
$needle = 'ly';
$result = array_find($needle,$words_array);
echo "<p>=============</p> ";
print_r($result);
function array_find($needle, array $haystack)
{
foreach ($haystack as $key => $value) {
if(substr($value, -2) != 'ly')
{
unset($haystack[$key]);
}
}
return $haystack;
}
preg_match_all('/[\W]+([a-zA-Z]+ly)[\W]+/', $desi, $matches);
$matches = $matches[1];
var_dump($matches);
This is a simple solution I've come up with using regular expression. It groups the words ending with ly
and adds them to the array of third parameter passed in preg_match_all
function.
Hope it helps!