如何在超过 170 个字符的第一个空格后添加省略号超链接? [英] How to add an ellipsis hyperlink after the first space beyond 170 characters?

查看:64
本文介绍了如何在超过 170 个字符的第一个空格后添加省略号超链接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像下面这样的长文本:

I have a long text like below:

$postText="It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy.";

我想在 170 个字符后添加 readmore 超链接不切断一个单词并包含一个尾随空格字符.

I want to add readmore hyperlink after 170 characters without cutting off a word and include a trailing whitespace character.

我的编码尝试:

if(strlen($postText)>170){
    $splitArr=preg_split("/.{170}\S*\s/",$postText,2);
    print_r($splitArr);
    exit;
    $postText=$splitArr[0]."...<a class='see-more' href='http://example.com/seemore-link'>read more</a>";
}

拆分数组总是将第一个索引返回为 null.我在 REGEX101 中检查了我的正则表达式,它显示了我需要的内容.请指出哪里出了问题.

Split array always return the first index as null. I checked my regex in REGEX101, and it shows exactly what I need. Please point out what is wrong.

推荐答案

拆分数组总是将第一个索引返回为空.

split array always return the first index as null.

它不返回 NULL,它返回一个空的 string ('');它们是完全不同的对象,具有不同的语义.

It doesn't return NULL, it returns an empty string (''); they are completely different objects with different semantics.

返回数组的第一个元素是空字符串的原因在preg_split():

The reason why the first element of the returned array is an empty string is clearly documented in the manual page of preg_split():

返回值:

返回一个数组,其中包含 subject 的子字符串,这些子字符串沿由 pattern 匹配的边界拆分,失败时返回 FALSE.

Returns an array containing substrings of subject split along boundaries matched by pattern, or FALSE on failure.

您作为 preg_split() 的第一个参数提供的正则表达式 用于匹配分隔符,而不是片段.你需要的函数是preg_match():

$postText = "It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy.";

preg_match('/^.{170}\S*/', $postText, $matches);

$postText = $matches[0] . " ...<a class='see-more' href='http://example.com/seemore-link'>read more</a>";

如果 preg_match() 返回 TRUE$matches[0] 包含您需要的字符串.

If preg_match() returns TRUE, $matches[0] contains the string you need.

在某些情况下,preg_match() 会因您的原始 regex 而失败.例如,如果您的输入字符串正好有 170 个字符,则 \s 将不匹配.这就是为什么我从 regex 中删除了 \s 并在匹配后附加的字符串前面添加了一个空格.

There are situations when preg_match() fails with your original regex. For example, if your input string has exactly 170 characters, the \s won't match. This is why I removed the \s from the regex and added a white space in front of the string appended after the match.

这篇关于如何在超过 170 个字符的第一个空格后添加省略号超链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆