所以我有这个字符串,我想输出所有结束于" 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.";
我怎样才能得到结尾的所有单词" ly"在这个字符串??
答案 0 :(得分:1)
这是一个解决方案。首先,我创建了一个包含所有单词的数组:
$words = explode(" ", $desi);
然后测试每个单词并检查它是否以" ly" (使用preg_match函数)
foreach($words as $word){
if (preg_match("/ly$/", $word)){
echo $word." ";
}
}
我打印出了单词,但你可以做任何你喜欢的事情,比如将它们保存在一个数组中......或者
注意:此代码未能明确选择""因为";"
答案 1 :(得分:1)
preg_match_all('/[\W]+([a-zA-Z]+ly)[\W]+/', $desi, $matches);
$matches = $matches[1];
var_dump($matches);
这是我用正则表达式提出的一个简单的解决方案。它将以ly
结尾的单词分组,并将它们添加到preg_match_all
函数中传递的第三个参数的数组中。
希望它有所帮助!
答案 2 :(得分:0)
试试这个解决方案
$needle = 'ly';
$all = explode(' ', $desi);
$selected = [];
foreach ($all as $key => $value) {
if (substr($value, -strlen($needle)) === (string) $needle) {
$selected[] = $value;
}
}
var_dump($selected);
答案 3 :(得分:0)
这可能对您有所帮助
$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;
}