dongmu2026 2015-04-02 10:25 采纳率: 100%
浏览 114

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

I am a novice at PHP I am just learning. But I have come across a problem that I am having real trouble with.

I am working inside WordPress with a plug-in and I am trying to shorten a string within that plug-in so it just displays a small amount of text instead of a large section.

I know from reading this website and other including PHP.net that I need to use 'substr' The main problem is I think I need to add more than the string.

In my working one that does not shorten there is something with the string in ['post_content'] I am not sure how that works with the syntax of PHP.

This is what I have come up with using the resources I found. Also adding ... to the end would be great.

<?php

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

And this is the working but without shortening

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

Resources

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条回答 默认 最新

  • dongzhi4690 2015-04-02 10:29
    关注
    <?php
    $property = $property['post_content'];
    if (strlen($property) > 15) // if you want...
    {
        $maxLength = 14;
        $property = substr($property, 0, $maxLength);
    }
    echo $property;
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器