我如何在字符串中获取单词,具体取决于最后 2 个字母 php



所以我有这个字符串,我想输出所有以"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"结尾的所有单词?

preg_match_all('/[W]+([a-zA-Z]+ly)[W]+/', $desi, $matches);
$matches = $matches[1];
var_dump($matches);

这是我使用正则表达式提出的一个简单的解决方案。它将以 ly 结尾的单词分组,并将它们添加到函数中传递preg_match_all第三个参数数组中。

希望对您有所帮助!

这是一个解决方案。首先,我做了一个包含所有单词的数组:

$words = explode(" ", $desi);

然后测试每个单词并检查它是否以"ly"结尾(使用 preg_match 函数)

foreach($words as $word){
    if (preg_match("/ly$/", $word)){
        echo $word." ";
    }
}

我打印了单词,但您可以做任何您喜欢的事情,例如将它们保存在数组中或......

注意:由于";",此代码无法选择"清晰"。

这可能会

对你有所帮助

$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;
}

试试这个解决方案

$needle = 'ly';
$all = explode(' ', $desi);
$selected = [];
foreach ($all as $key => $value) {
    if (substr($value, -strlen($needle)) === (string) $needle) {
        $selected[] = $value;
    }
}
var_dump($selected);

最新更新