英文:
Create post excerpts in PHP
问题
以下是翻译好的部分:
开发者编写了以下PHP代码来创建文章摘要,如下所示:
/* 此函数允许自动创建文章摘要
======================================================================== */
function truncate_post_simple($amount,$quote_after=false) {
$truncate = get_the_content();
$truncate = strip_shortcodes( $truncate );
$truncate = strip_tags($truncate);
$truncate = substr($truncate, 0, strrpos(substr($truncate, 0, $amount), '' '));
echo $truncate;
echo "...";
if ($quote_after) echo('');
}
我无法理解以下这行代码:
$truncate = substr($truncate, 0, strrpos(substr($truncate, 0, $amount), '' '));
看起来它提取了文章的前部分,长度为$amount个字符,然后删除以空格开头的额外部分。
我不理解为什么他要这样做。为什么不直接使用以下方式:
$truncate = substr($truncate, 0, $amount);
或者如果他想要去除额外的空格,为什么不使用:
$truncate = rtrim(substr($truncate, 0, $amount));
英文:
A develper wrote the following PHP code to create the post excerpt, as below:
/* This function allows for the auto-creation of post excerpts
======================================================================== */
function truncate_post_simple($amount,$quote_after=false) {
$truncate = get_the_content();
$truncate = strip_shortcodes( $truncate );
$truncate = strip_tags($truncate);
$truncate = substr($truncate, 0, strrpos(substr($truncate, 0, $amount), ' '));
echo $truncate;
echo "...";
if ($quote_after) echo('');
}
I cannot understand the following line:
$truncate = substr($truncate, 0, strrpos(substr($truncate, 0, $amount), ' '));
It seems that the extraction is the first part of the post with $amount of characters, and then it will remove an extra part that beginning with a space.
I cannot understand why he will do this. Why don't use
$truncate = substr($truncate, 0, $amount);
directly, or if he wants to remove extra spaces, using:
$truncate = $rtrim(substr($truncate, 0, $amount));
答案1
得分: 2
它看起来他们试图找到在截断前的第$amount
个字符之前的最后一个空格。这样,单词就不会在中间被截断。你可以按如下方式拆分它:
// 在$truncate中查找最后一个空格,该空格位于第$amount个字符之前
$lastSpace = strrpos(substr($truncate, 0, $amount), ' ');
// 截断字符串到最后一个空格
$truncate = substr($truncate, 0, $lastSpace);
一些示例可能有所帮助:
$truncate = "The quick brown fox jumps over the lazy dog";
$amount = 12;
$lastSpace = strrpos(substr($truncate, 0, $amount), ' '); // 9
$result = substr($truncate, 0, $lastSpace); // "The quick"
如果我们只是直接使用了$amount
,单词"brown"会被截断在中间:
$result = substr($truncate, 0, $amount); // "The quick br";
英文:
It looks like they were trying to find the last space before the $amount
-th character to be truncated. This way, a word is not clipped in the middle. You can break it down as follows:
// finds the last space in $truncate before the $amount-th character
$lastSpace = strrpos(substr($truncate, 0, $amount), ' ');
// truncate the string to the last space
$truncate = substr($truncate, 0, $lastSpace);
Some examples may help:
$truncate = "The quick brown fox jumps over the lazy dog";
$amount = 12;
$lastSpace = strrpos(substr($truncate, 0, $amount), ' '); // 9
$result = substr($truncate, 0, $lastSpace); // "The quick"
If we had just used $amount
directly, the word "brown" would have been
clipped in the middle
$result = substr($truncate, 0, $amount); // "The quick br";
答案2
得分: 2
将其展开并加以注释。
$truncate = substr( // 从0开始截取到$amount之前的第一个空格
$truncate,
0,
strrpos( // 第一个空格的位置,从子字符串末尾开始计算
substr( // 盲目截取长度为$amount的子字符串,从0开始
$truncate,
0,
$amount
),
' '
)
);
英文:
Spread it out and annotate it.
$truncate = substr( // substring from 0 until the first space before $amount
$truncate,
0,
strrpos( // position of the first space, counted from the end of the substring.
substr( // blind substring of length $amount, starting from 0
$truncate,
0,
$amount
),
' '
)
);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论