0

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 !

Community
  • 1
  • 1
Jonathan Ross
  • 187
  • 1
  • 7

1 Answers1

0

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

Martin Hennings
  • 15,707
  • 9
  • 41
  • 66
  • Thanks for your help, Martin. Your final snippet is exactly what causes your second snippet, if you know what I mean. In other other words your third snippet is exactly what I have and what breaks the Short Link output and causes the spill of text. – Jonathan Ross Jul 13 '11 at 15:09
  • Does this shed any light ? http://stackoverflow.com/questions/6527880/php-wordwrap-cut-parameter-when-dealing-with-weird-characters – Jonathan Ross Jul 13 '11 at 20:25
  • Thanks, I've tried a few of these from the man page with no luck :( – Jonathan Ross Jul 14 '11 at 06:39
  • @JonathanRoss let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/1444/discussion-between-martin-and-jonathan-ross) – Martin Hennings Jul 14 '11 at 07:30
  • I'll have some time either late today or tomorrow. Thanks again !! – Jonathan Ross Jul 14 '11 at 09:33