Wordwrap + possible regexp help

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Hurreman
Forum Commoner
Posts: 61
Joined: Sat Apr 29, 2006 8:42 am

Wordwrap + possible regexp help

Post 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;
    }
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Re: Wordwrap + possible regexp help

Post 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
Last edited by aerodromoi on Mon May 15, 2006 11:41 pm, edited 1 time in total.
User avatar
Hurreman
Forum Commoner
Posts: 61
Joined: Sat Apr 29, 2006 8:42 am

Post 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.
User avatar
aerodromoi
Forum Contributor
Posts: 230
Joined: Sun May 07, 2006 5:21 am

Post 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
Post Reply