Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将标题修剪为最接近的单词_Php_Jquery_Wordpress Theming - Fatal编程技术网

Php 将标题修剪为最接近的单词

Php 将标题修剪为最接近的单词,php,jquery,wordpress-theming,Php,Jquery,Wordpress Theming,例如,假设我有以下代码: <h3>My very long title</h3> <h3>Another long title</h3> 我的很长的标题 又一个长标题 如果我想使用PHP或jQuery缩短这些标题,我如何将它们修剪到最近的单词并附加省略号?是否可以指定字符计数 <h3>My very long...</h3> <h3>Another long...</h3> 我的很长时间。。。

例如,假设我有以下代码:

<h3>My very long title</h3>
<h3>Another long title</h3>
我的很长的标题
又一个长标题
如果我想使用PHP或jQuery缩短这些标题,我如何将它们修剪到最近的单词并附加省略号?是否可以指定字符计数

<h3>My very long...</h3>
<h3>Another long...</h3>
我的很长时间。。。
又一个长长的。。。
编辑- 我怎样才能为每一个标题做到这一点?我真的不知道如何将每个标题转换成字符串

谢谢

见此:

编辑:(包括


然后可按以下方式使用此功能:

<?php
$text = "Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.";
echo ellipsis($text,100);
?>


也许您正在搜索

使用$break可使用可选的break参数打断该行。 如果cut设置为TRUE,则字符串总是以指定宽度或之前的宽度进行包装。因此,如果您有一个大于给定宽度的单词,它将被拆分

查看php网站上的函数文档,了解更多示例

+++


另一种解决方案是使用
'
(一个空格)拆分标题,并提供最多5个单词的限制,而不是使用array\u pop切断数组的最后一个元素,最后使用
'
(该空格)将它们连接起来但是这个解决方案并不是最好的,因为如果你有很长的单词,它会给你难看的输出。

如果你想在一个特定的长度内,这会起作用

function truncateString($string, $length, $append = '...') {
    $length -= strlen($append); // length of "..."

    $string = substr($string, 0, $length);
    $string = substr($string, 0, strrpos($string, ' '));
    $string .= $append;

    return $string;
}
echo truncateString('My very long title', 15);
经过测试,效果完美


编辑:变成函数。

使用PHP函数很容易。请看以下示例:

function trim_text($input, $length) {

    // If the text is already shorter than the max length, then just return unedited text.
    if (strlen($input) <= $length) {
        return $input;
    }

    // Find the last space (between words we're assuming) after the max length.
    $last_space = strrpos(substr($input, 0, $length), ' ');
    // Trim
    $trimmed_text = substr($input, 0, $last_space);
    // Add ellipsis.
    $trimmed_text .= '...';

    return $trimmed_text;
}
function trim_text($input,$length){
//如果文本已经短于最大长度,则只返回未编辑的文本。

if(strlen($input)使用一些php和强大的正则表达式

function formatHeadline($headline){
    if(preg_match('/(\w+\s+){3}/', $subject, $match)){
        return $match[1]  . '...';
    }
    return $headline;
}
在使用regex和jquery的javascript中也可以使用相同的方法


Jquery也有这个插件,你可能想看看它。

只是一句话,OP不想把单词一分为二。最近的单词它说“如果剪切设置为真”,所以他只需要把它设为假。我嘲笑回答这个问题的竞争力。@thirdydot:这在这些简单的问题上发生。@thirdydot,这是我们可以在几秒钟内得到答案的网站
string wordwrap ( string $str [, int $width = 75 [, string $break = "\n" [, bool $cut = false ]]] )
function truncateString($string, $length, $append = '...') {
    $length -= strlen($append); // length of "..."

    $string = substr($string, 0, $length);
    $string = substr($string, 0, strrpos($string, ' '));
    $string .= $append;

    return $string;
}
echo truncateString('My very long title', 15);
function trim_text($input, $length) {

    // If the text is already shorter than the max length, then just return unedited text.
    if (strlen($input) <= $length) {
        return $input;
    }

    // Find the last space (between words we're assuming) after the max length.
    $last_space = strrpos(substr($input, 0, $length), ' ');
    // Trim
    $trimmed_text = substr($input, 0, $last_space);
    // Add ellipsis.
    $trimmed_text .= '...';

    return $trimmed_text;
}
function formatHeadline($headline){
    if(preg_match('/(\w+\s+){3}/', $subject, $match)){
        return $match[1]  . '...';
    }
    return $headline;
}