开发者

Break-up-one-line-of-text-without-any-discernible-break-points-in-php [part 2]

开发者 https://www.devze.com 2023-03-20 03:05 出处:网络
I received three really helpful answers here: Break up one line of text without any discernible break points in PHP

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 !


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号