在 php 中分解一行文本而无任何可识别的断点 [第 2 部分]

发布于 2024-11-19 22:19:44 字数 1197 浏览 1 评论 0原文

我在这里收到了三个非常有用的答案:

在 PHP 中分解一行文本,没有任何明显的断点

它似乎添加 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痕至 2024-11-26 22:19:44

首先,看一下 wordwrap 手册页。在那里,在用户贡献的注释部分中,您可以找到自动换行函数的代码,该函数取出所有 html 标签,自动换行其余部分并将 html 标签放回原处。

然后要回答您的问题,请尝试调试通过编辑您的问题来位并分享您的结果。尝试什么:

提供的示例应该如下所示:

<a href="http://www.vodafone.co.uk/vodafone-uk/about-this-site/terms-and-conditions/products-and-services/bonus-bank/index.htm" 
   title="http://www.vodafone.co.uk/vodafone-uk/about-this-site/terms-and-conditions/products-and-services/bonus-bank/index.htm" 
   target=_blank><i>Short Link</i></font></a> 

但看起来像这样,对吗?

<a href="http://www.vodafone.co.uk/vodafone-uk/about-this-site/terms-and-conditions/products" 
   title="http://www.vodafone.co.uk/vodafone-uk/about-this-site/terms-and-conditions/products" 
   target=_blank><i>Short Link</i></font></a> 
-and-services/bonus-bank/index.htm

因此,首先看看 wordwrap 对此做了什么:

$teststring = '<a href="http://www.vodafone.co.uk/vodafone-uk/about-this-site/terms-and-conditions/products-and-services/bonus-bank/index.htm" 
   title="http://www.vodafone.co.uk/vodafone-uk/about-this-site/terms-and-conditions/products-and-services/bonus-bank/index.htm" 
   target=_blank><i>Short Link</i></font></a>';
echo wordwrap($teststring, 83, "\n", true);

如果您已经这样做了,请将结果的 html 代码作为代码放入您的问题中。


尝试这个函数(来自wordwrap的php手册页):

这里有代码,但质量不好

我没有测试它,但如果它有效,你很幸运,对吧?


现在你让我尝试了一下,这就是我的结果:

function get_tags_array($str)
{
    //given a string, return a sequential array with html tags in their own elements
    $q = '?';
    return preg_split("/(<.*$q>)/",$str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
}

function html_combi_wordwrap($str, $width = 75, $break = "\n", $cut = false)
{
    $tag_arr = get_tags_array($str);
    foreach($tag_arr as $key => $tag_str)
    {
        if(preg_match("/<.*>/", $tag_str))
            continue;

        $tag_arr[$key] = wordwrap($tag_str, $width, $break, $cut);
    }
    return implode($tag_arr);
}

这结合了此代码(即,仅将没有 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:

<a href="http://www.vodafone.co.uk/vodafone-uk/about-this-site/terms-and-conditions/products-and-services/bonus-bank/index.htm" 
   title="http://www.vodafone.co.uk/vodafone-uk/about-this-site/terms-and-conditions/products-and-services/bonus-bank/index.htm" 
   target=_blank><i>Short Link</i></font></a> 

but looks like this, right?

<a href="http://www.vodafone.co.uk/vodafone-uk/about-this-site/terms-and-conditions/products" 
   title="http://www.vodafone.co.uk/vodafone-uk/about-this-site/terms-and-conditions/products" 
   target=_blank><i>Short Link</i></font></a> 
-and-services/bonus-bank/index.htm

So, first see what wordwrap does with that:

$teststring = '<a href="http://www.vodafone.co.uk/vodafone-uk/about-this-site/terms-and-conditions/products-and-services/bonus-bank/index.htm" 
   title="http://www.vodafone.co.uk/vodafone-uk/about-this-site/terms-and-conditions/products-and-services/bonus-bank/index.htm" 
   target=_blank><i>Short Link</i></font></a>';
echo wordwrap($teststring, 83, "\n", true);

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:

function get_tags_array($str)
{
    //given a string, return a sequential array with html tags in their own elements
    $q = '?';
    return preg_split("/(<.*$q>)/",$str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
}

function html_combi_wordwrap($str, $width = 75, $break = "\n", $cut = false)
{
    $tag_arr = get_tags_array($str);
    foreach($tag_arr as $key => $tag_str)
    {
        if(preg_match("/<.*>/", $tag_str))
            continue;

        $tag_arr[$key] = wordwrap($tag_str, $width, $break, $cut);
    }
    return implode($tag_arr);
}

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文