我有以下代码:
$caption = $picture->getCaption();
$words = explode(" ", $caption);
foreach ($words as $word) {
$string_length = strlen($word);
if ($string_length > 40) {
str_replace($word, '', $caption);
$picture->setCaption($caption);
}
}
但是,为什么这不会用删除的文字替换标题呢?
最佳答案
你需要这样做:
$caption = $picture->getCaption();
$words = explode(" ", $caption);
foreach ($words as $word)
{
$string_length = strlen($word);
if ($string_length > 40) {
$picture->setCaption(str_replace($word, '', $caption));
}
}
关于php - 替换字符串中长度为 40 或以上的单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26521235/