Page 1 of 1

Wordwrap + possible regexp help

Posted: Mon May 15, 2006 2:39 pm
by Hurreman
Lately I've been developing a forum for some friends, and have implemented some simple bbcode support for the classics bold, italic, underline, image and urls. When it came to splitting long words to prevent them from messing up the layout, I thought wordwrap would be enough, but I didn't think about the bbcode.

Example:

Code: Select all

$string = "This is a very long [url=http://www.thisisaverylonglink.com]link[/url].";
echo wordwrap($string, 25, "",1);
Would output:

Code: Select all

This is a very long <a href="http://www.thisisave rylonglink.com">link</a>.
Causing the link to break. In some cases it would insert a whitespace into the [/url] element, which would break the regexp generating the links.

Being the regexp newbie I am and not even being sure if regexp is the solution, I came running here.
Does anyone have some suggestions? I'll post my code for generating the BBCODE elements so you can see what's going on.

Code: Select all

function prepareString($string)
    {
        // URL
        $urlPattern1 = "!\\[url=(.*?)\\](.*?)\\[/url\\]!i";
        if(preg_match($urlPattern1,$string))
        {
            $string = preg_replace("!\\[url=(.*?)\\](.*?)\\[/url\\]!i", "<a target=\"_blank\" href=\"\\1\">\\2</a>", $string);
        }
        $urlPattern2 = '/\[url\](.*?)\[\/url\]/i';
        if(preg_match($urlPattern2,$string))
        {
            $string = preg_replace('/\[url\](.*?)\[\/url\]/i','<a target="blank" href="$1">$1</a>',$string);
        }

        // Image
        $pattern = '/\[img\](.*?)\[\/img\]/i';
        $replace = '<img class="pageImage" src="$1">';
        $string = preg_replace($pattern, $replace, $string);


        // Setup paragraphs
        $pArr = explode(chr(13),$string);
        $newStr = null;
        foreach($pArr as $paragraph)
        {
            if(trim($paragraph)=='')
            {
                $newStr .= '<br/>';
            }
            else
            {
                $newStr .= "<p>$paragraph</p>";
            }
        }

        return $newStr;
    }

Re: Wordwrap + possible regexp help

Posted: Mon May 15, 2006 3:29 pm
by aerodromoi
Hurreman wrote: Example:

Code: Select all

$string = "This is a very long [url=http://www.thisisaverylonglink.com]link[/url].";
echo wordwrap($string, 25, " ",1);
By setting [int cut] to 1 you're telling wordwrap to insert a whitespace every 25 characters irrespective of whether a word is broken apart or not.

aerodromoi

Posted: Mon May 15, 2006 6:43 pm
by Hurreman
The reason I used [int cut] was to cut words being longer than 25 characters, as long words might break the layout.
There're always people posting insanely long lines just to mess around, and when they do, I'd like to prevent it from messing up the table width.

Posted: Tue May 16, 2006 11:10 am
by aerodromoi
Hurreman wrote:The reason I used [int cut] was to cut words being longer than 25 characters, as long words might break the layout.
There're always people posting insanely long lines just to mess around, and when they do, I'd like to prevent it from messing up the table width.
Just an idea...

Code: Select all

<?php
$string = "Loremipsumdolorsitamet,consectetueradipiscingelit. Nullam tincidunt augue in 
  lacus. Vivamus elit pede, http://www.scelerisqueaplaceratamollisquismagna.com sociis natoque penatibus 
  et magnis dis parturient montes, nascetur ridiculus mus. Mauris felis nunc, sollicitudin molestie, 
  adipiscing vel, nonummy non, tellus.";
  
  
function wrapf($string){
  $wordarray = explode(" ",$string);
  
  for ($i=0;$i<count($wordarray);$i++){
     if (strlen($wordarray[$i]) > 25){
       if (!ereg("http://", $wordarray[$i]) OR strlen($wordarray[$i]) > 255){
          $wordarray[$i] = wordwrap($wordarray[$i], 25, " ",1); 
       }
     }
  }
  $string = implode(" ", $wordarray);
  
  return $string;
}

echo wrapf($string);
?>
aerodromoi