Text parsing

How to break long lines, get BBcode etc.

Text parsing

Postby blodan on 23 Nov 2007, 01:23

So, you want some BBcode and so on to your comments/forum or whatever? This is definitely the right thread for that!

BBcode
  1. function bbcode($text) {
  2.     $text = preg_replace("!\\[url=(.*?)\\](.*?)\\[/url\\]!i", "<a href=\"\\1\" target=\"_blank\">\\2</a>", $text);
  3.     $text = preg_replace("!\\[link=(.*?)\\](.*?)\\[/link\\]!i", "<a href=\"\\1\" target=\"_blank\">\\2</a>", $text);
  4.     $text = preg_replace("!\\[b\\](.*?)\\[/b\\]!i", "<strong>\\1</strong>", $text);
  5.     $text = preg_replace("!\\[u\\](.*?)\\[/u\\]!i", "<u>\\1</u>", $text);
  6.     $text = preg_replace("!\\[i\\](.*?)\\[/i\\]!i", "<i>\\1</i>", $text);
  7.     preg_match_all("!\\[img\\](.*?)\\[/img\\]!i", $text, $out, PREG_SET_ORDER);
  8.     foreach($out as $fixthis) {
  9.         $string = $fixthis[1];
  10.         $sizen = getimagesize($string);
  11.         if($sizen[0] > 300) { $width = 300; }else{ $width = $sizen[0]; } //if you want the maxwidth to be something else then 300px, just change the value
  12.         $text = str_replace("[img]".$string."[/img]", "<img src=\"".$string."\" style=\"width: ".$width."px;\" alt=\"\" />", $text);
  13.     }
  14.     $text = quotes($text);
  15.     return $text;
  16. }
  17. function quotes($input)
  18. {
  19.     $regex = '#\[quote=([^]]+)]((?:[^[]|\[(?!/?quote[^]]*])|(?R))+)\[/quote]#';
  20.     if (is_array($input)) {
  21.         $input = "\r\n<table cellpadding=\"0\" cellspacing=\"0\"style=\"width: 100%;\"><tr><td style=\"width: 30px;\">&nbsp;</td><td><strong>".$input[1]."</strong> said:</td></tr><tr><td style=\"width: 30px;\">&nbsp;</td><td style=\"border: 1px #000000 dotted; background-color: #ecebe7; padding: 5px 5px 5px 5px;\">".$input[2]."</td></tr></table>";
  22.        
  23.     }
  24.  
  25.     return preg_replace_callback($regex, 'quotes', $input);
  26. }


Make links imbedded in text
  1. function makelinks($text) {
  2.     $text = preg_replace("/(?<=^|\s)(www\.([-a-zA-Z0-9_]+\.)*[-a-zA-Z0-9_]+\.[-a-zA-Z0-9_]{2,6}(\/[^\s]*)*)(?=\s|$)/", "http://$1", $text);
  3.     $search = "/(?<=^|\s)(http:\/\/|https:\/\/|ftp:\/\/)(([-a-zA-Z0-9_]+\.)*[-a-zA-Z0-9_]+\.[-a-zA-Z0-9_]{2,6}(\/[^\s]*)*)(?=\s|$|\.\s|\.$|,\s|,$|\!\s|\!$)/U";
  4.     $replace = "<a href=\"$1$2\" target=\"_blank\">$2</a>";
  5.     $text = preg_replace($search, $replace, $text);
  6.     return $text;
  7. }


Break long words
  1. function breakLongWords($str, $maxLength, $char){
  2.     $wordEndChars = array(" ", "\n", "\r", "\f", "\v", "\0");
  3.     $count = 0;
  4.     $newStr = "";
  5.     $openTag = false;
  6.     for($i=0; $i<strlen($str); $i++){
  7.         $newStr .= $str{$i};    
  8.        
  9.         if($str{$i} == "<"){
  10.             $openTag = true;
  11.             continue;
  12.         }
  13.         if(($openTag) && ($str{$i} == ">")){
  14.             $openTag = false;
  15.             continue;
  16.         }
  17.        
  18.         if(!$openTag){
  19.             if(!in_array($str{$i}, $wordEndChars)){//If not word ending char
  20.                 $count++;
  21.                 if($count==$maxLength){//if current word max length is reached
  22.                     $newStr .= $char;//insert word break char
  23.                     $count = 0;
  24.                 }
  25.             }else{//Else char is word ending, reset word char count
  26.                     $count = 0;
  27.             }
  28.         }
  29.        
  30.     }//End for    
  31.     return $newStr;
  32. }


I recommend using them in this order though:
  1. echo breakLongWords(bbcode(nl2br(makelinks(htmlentities($text)))),65,"<br />");
User avatar
blodan
Site Admin
 
Posts: 27
Joined: 17 Nov 2007, 00:16

Return to Posts/Comments/Replys

Who is online

Users browsing this forum: No registered users and 1 guest

cron


  © 2007-2009 PRAXXA HB, Org. nr 969727-7227 Driven by Daniel Ylitalo & Daniel Lindholm, info@praxxa.com  
  The PRAXXA Network » www.praxxa.com » www.phpBNC.se » www.phpguides.eu » www.phpstuff.org » Nyheter