我在php中有这个字符串
$string="lorem-ipsum-dolor-sit-amet-consectetur-adipiscing-elit-sed-do-eiusmod-tempor-incididunt-ut-labore-et-dolore-magna-aliqua";
在这种情况下,我只想检索-
之间的六个开头的单词
function retrieve6firstWords($string){
return $string;
}
echo retrieve6firstWords($string); //$string= "lorem-ipsum-dolor-sit-amet-consectetur";
我该怎么做?
答案 0 :(得分:0)
explode乘-,slice的array只有6,而implode又回到string。
<?php
$string = "lorem-ipsum-dolor-sit-amet-consectetur-adipiscing-elit-sed-do-eiusmod-tempor-incididunt-ut-labore-et-dolore-magna-aliqua";
$words = explode('-', $string);
$words = array_slice($words, 0, 6);
$string = implode('-', $words);
print_r($string);
结果:
lorem-ipsum-dolor-sit-amet-consectetur