首页 文章

substr PHP缩短字符串的长度(WordPress插件)

提问于
浏览
1

我是PHP的新手,我只是在学习 . 但我遇到了一个问题,我遇到了麻烦 .

我正在使用插件在WordPress中工作,我试图缩短该插件中的字符串,因此它只显示少量文本而不是大部分 .

我知道从阅读本网站和其他包括PHP.net在内我需要使用'substr'主要问题是我认为我需要添加更多的字符串 .

在我的工作中,没有缩短,['post_content']中的字符串有一些东西我不知道如何使用PHP的语法 .

这就是我用我找到的资源想出来的 . 另外添加......到最后会很棒 .

<?php

if (strlen($property) > 15) // if you want...
{
    $maxLength = 14;
    $property = substr($property, 0, $maxLength);
}
?>

这是工作但没有缩短

<?php echo $property['post_content']; ?>

资源

Get first n characters of a string

Shorten a text string in PHP

Shorten string with a "..." body

http://php.net/manual/en/function.substr.php

3 回答

  • 0
    <?php
    $property = $property['post_content'];
    if (strlen($property) > 15) // if you want...
    {
        $maxLength = 14;
        $property = substr($property, 0, $maxLength);
    }
    echo $property;
    ?>
    
  • 0
    <?php
    if (strlen($property['post_content']) > 15) // if you want...
    {
       $mystring= substr($property['post_content'], 0,14);
    }
    echo $mystring; 
    ?>
    
  • 0

    试试这个

    $property = (strlen($property) > 15) ? substr($property,0,14): $property ;
    

相关问题