php - 替换字符串中长度为 40 或以上的单词

标签 php string

我有以下代码:

$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/

相关文章:

php - 将联系表发送到个人电子邮件的问题

PHP 表单在创建用户帐户时将 NULL 行添加到我的 SQL 数据库

php - 如何在mysql中选择前三行

c++ - 如何在字符串中搜索 C++

javascript - 使用 angular JS 和 ionic 从 select 中获取选定的选项

php - EasyPHP 上的激活 curl

java - 在 Java 中加入字符串集合的首选习惯用法

c++ - 使用迭代器在字符串中查找子字符串

Java 仅使用 charAt 连接两个字符串

c# - 如何按位置替换字符串的一部分?