无法使 PHP substr 函数工作

Cannot get PHP substr function to work

我想截断下面输出简单文本行的代码:

<?php echo $post->author->getTitle();?>

所以我这样做了:

<?php $post->author->getTitle();
   echo substr($post, 0, 15);
?>

但是,我明白了:

Warning: substr() expects parameter 1 to be string, object given in /entry/default.php on line 107

知道如何解决吗?

替换:

substr($post, 0, 15);

与:

substr($post->author->getTitle(),0,15);

您还没有将 $post->author->getTitle() 的值转换为变量 $post 因此您必须执行 $post = $post->author->getTitle() 然后 substr($post,0,15)