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

时间:2015-04-02 10:25:14

标签: php string wordpress substr

我是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 :(得分:0)

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

试试这个

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