BBcode
- function bbcode($text) {
- $text = preg_replace("!\\[url=(.*?)\\](.*?)\\[/url\\]!i", "<a href=\"\\1\" target=\"_blank\">\\2</a>", $text);
- $text = preg_replace("!\\[link=(.*?)\\](.*?)\\[/link\\]!i", "<a href=\"\\1\" target=\"_blank\">\\2</a>", $text);
- $text = preg_replace("!\\[b\\](.*?)\\[/b\\]!i", "<strong>\\1</strong>", $text);
- $text = preg_replace("!\\[u\\](.*?)\\[/u\\]!i", "<u>\\1</u>", $text);
- $text = preg_replace("!\\[i\\](.*?)\\[/i\\]!i", "<i>\\1</i>", $text);
- preg_match_all("!\\[img\\](.*?)\\[/img\\]!i", $text, $out, PREG_SET_ORDER);
- foreach($out as $fixthis) {
- $string = $fixthis[1];
- $sizen = getimagesize($string);
- 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
- $text = str_replace("[img]".$string."[/img]", "<img src=\"".$string."\" style=\"width: ".$width."px;\" alt=\"\" />", $text);
- }
- $text = quotes($text);
- return $text;
- }
- function quotes($input)
- {
- $regex = '#\[quote=([^]]+)]((?:[^[]|\[(?!/?quote[^]]*])|(?R))+)\[/quote]#';
- if (is_array($input)) {
- $input = "\r\n<table cellpadding=\"0\" cellspacing=\"0\"style=\"width: 100%;\"><tr><td style=\"width: 30px;\"> </td><td><strong>".$input[1]."</strong> said:</td></tr><tr><td style=\"width: 30px;\"> </td><td style=\"border: 1px #000000 dotted; background-color: #ecebe7; padding: 5px 5px 5px 5px;\">".$input[2]."</td></tr></table>";
- }
- return preg_replace_callback($regex, 'quotes', $input);
- }
Make links imbedded in text
- function makelinks($text) {
- $text = preg_replace("/(?<=^|\s)(www\.([-a-zA-Z0-9_]+\.)*[-a-zA-Z0-9_]+\.[-a-zA-Z0-9_]{2,6}(\/[^\s]*)*)(?=\s|$)/", "http://$1", $text);
- $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";
- $replace = "<a href=\"$1$2\" target=\"_blank\">$2</a>";
- $text = preg_replace($search, $replace, $text);
- return $text;
- }
Break long words
- function breakLongWords($str, $maxLength, $char){
- $wordEndChars = array(" ", "\n", "\r", "\f", "\v", "\0");
- $count = 0;
- $newStr = "";
- $openTag = false;
- for($i=0; $i<strlen($str); $i++){
- $newStr .= $str{$i};
- if($str{$i} == "<"){
- $openTag = true;
- continue;
- }
- if(($openTag) && ($str{$i} == ">")){
- $openTag = false;
- continue;
- }
- if(!$openTag){
- if(!in_array($str{$i}, $wordEndChars)){//If not word ending char
- $count++;
- if($count==$maxLength){//if current word max length is reached
- $newStr .= $char;//insert word break char
- $count = 0;
- }
- }else{//Else char is word ending, reset word char count
- $count = 0;
- }
- }
- }//End for
- return $newStr;
- }
I recommend using them in this order though:
- echo breakLongWords(bbcode(nl2br(makelinks(htmlentities($text)))),65,"<br />");
