在 php 中分解一行文本而无任何可识别的断点 [第 2 部分]
我在这里收到了三个非常有用的答案:
它似乎添加 true
作为选项wordwrap 解决了这个问题,但它严重破坏了另一个功能,我无法使用它。该函数与此问题的思路相同,抓取长 URL 并将锚文本截断为“短 URL”。
任何想法为什么自动换行 true
添加会破坏这个?我看到在“短 URL”之后打印的 URL 设置限制之后有任何字符,这很奇怪。
function long_links($stringa){
$m = preg_match_all('/(www\.|http:\/\/|https:\/\/)([^\s]+)/', $stringa, $match);
if ($m){ $links=$match[0];
for ($j=0;$j<$m;$j++){
$loco=$links[$j]; $len=strlen($loco);
if($len > 59){
$stringa = str_replace($loco,'<a href="'.$loco.'" title="'.$loco.'" target=_blank><i>Short Link</i></a>',$stringa);
}
}
return $stringa;
}
为什么这会破坏 $body = wordwrap($body, 83, "\n", true);回声 $body;
?
我看到的溢出示例是:
*短网址*conditions/products-and-services/bonus-bank/index.htm
根据 Martin 的问题进行编辑
$body=long_links($body); $body = wordwrap($body, 83, "\n", true); echo $body;
再次感谢!
I received three really helpful answers here:
Break up one line of text without any discernible break points in PHP
It appeared adding true
as as option to wordwrap
fixed the problem but it's breaking another function so badly I can't use it. The function is along the same vein as this issue and grabs long URLs and truncates the anchor text to "Short URL".
Any ideas why the wordwrap true
addition breaks this ? I see any characters after the set limit of the URL printed after "Short URL" which is quite strange.
function long_links($stringa){
$m = preg_match_all('/(www\.|http:\/\/|https:\/\/)([^\s]+)/', $stringa, $match);
if ($m){ $links=$match[0];
for ($j=0;$j<$m;$j++){
$loco=$links[$j]; $len=strlen($loco);
if($len > 59){
$stringa = str_replace($loco,'<a href="'.$loco.'" title="'.$loco.'" target=_blank><i>Short Link</i></a>',$stringa);
}
}
return $stringa;
}
Why would this break $body = wordwrap($body, 83, "\n", true); echo $body;
?
An example of the spill over I see is:
*Short URL* conditions/products-and-services/bonus-bank/index.htm
EDIT following Martin's question
$body=long_links($body); $body = wordwrap($body, 83, "\n", true); echo $body;
Thanks again !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,看一下 wordwrap 手册页。在那里,在用户贡献的注释部分中,您可以找到自动换行函数的代码,该函数取出所有 html 标签,自动换行其余部分并将 html 标签放回原处。
然后要回答您的问题,请尝试调试通过编辑您的问题来位并分享您的结果。尝试什么:
提供的示例应该如下所示:
但看起来像这样,对吗?
因此,首先看看 wordwrap 对此做了什么:
如果您已经这样做了,请将结果的 html 代码作为代码放入您的问题中。
尝试这个函数(来自wordwrap的php手册页):
这里有代码,但质量不好
我没有测试它,但如果它有效,你很幸运,对吧?
现在你让我尝试了一下,这就是我的结果:
这结合了此代码(即,仅将没有 html 标签的文本换行)与常规换行功能,该功能仍然可以被 utf-8 变体或其他内容替换...
First of all, have a look at the wordwrap man page. There, in the section User Contributed Notes you can find code for a wordwrap function which takes out all html tags, wordwraps the rest and puts the html tags back in.
Then to answer your question, try debugging a bit and share your results by editing your question. What to try:
The provided example should look like this:
but looks like this, right?
So, first see what wordwrap does with that:
If you've done that, please put the html code of the result, as code as it is, into your question.
Try this function (from the php man page for wordwrap):
there was code here, but it was of no good quality
I didn't test it, but if it works, you're lucky, right?
Now you got me to try around a bit and this is my result:
This combines the idea of this code (which is, only wrap text that's no html tags) with the regular wordwrap function which can still be replaced by an utf-8 variant or whatever...