SNOWmanx
12-20-2006, 12:11 AM
Supposedly, I need to use preg_replace in my script. However, I don't know how it works.
<?php
$output = "XDgoogle.com";
$find = array(
"'\[b\](.*?)\[/b\]'is",
"'\[link\](.*?)\[/link\]'i",
"'\[link=(.*?)\](.*?)\[/link\]'i"
);
$replace = array(
"<strong>\\1</strong>",
"<a href=\"\\1\">\\1</a>",
"<a href=\"\\1\">\\2</a>"
);
preg_replace($find,$replace,$output);
?>
I need more focus on those arrays. I see a whole lot of backslashes... and I don't know what they mean. Also, I don't get the 1 and 2. Someone please help!
azizny
12-20-2006, 10:12 AM
The back slashes are to escape (treat as a character and not a modifier (a syntax used for certain options)).
The 1,2,3 etc.. represent what you are looking for (within the () modifiers), for example in this:
'\[b\](.*?)\[/b\]'is
Whatever is matched in (.?*) will be $1, if you had another () after that, it would be $2 and so on.
The above doesn't seem to be correct, here is my BBCode function (Two other functions to do quotes and smilies):
//Print text BBcoded instead
function print_bbcode($text){
$text = htmlentities($text);
//Serach for
$simple_search = array(
'/\[b\](.*?)\[\/b\]/is', //Bold
'/\[i\](.*?)\[\/i\]/is', //Italic
'/\[u\](.*?)\[\/u\]/is', //Underline
'/\[img\](.*?)\[\/img\]/is', //Image
'/\[url\](.*?)\[\/url\]/is', //Href link
'/\[url\=(.*?)\](.*?)\[\/url\]/is', //Href link+title
'/\[align\=(left|center|right|justify)\](.*?)\[\/align\]/is', //Alignment
'/\[mail\](.*?)\[\/mail\]/is', //Mail link
'/\[mail\=(.*?)\](.*?)\[\/mail\]/is', //Mail link + title
'/\[font\=(.*?)\](.*?)\[\/font\]/is', //Font color
'/\[size\=(.*?)\](.*?)\[\/size\]/is', //Size font
'/\[color\=(.*?)\](.*?)\[\/color\]/is', //Color font
'/\[code\](.*?)\[\/code\]/is', //Code
'/\[br\]/is', //Line Break (hr)
"/\n/is" //LNew Linesine Break (hr)
);
//Replace by
$simple_replace = array(
'<strong>$1</strong>',
'<em>$1</em>',
'<u>$1</u>',
'<a href="$1" target="_blank"><img src="$1" resizemod="on" onload="rmw_img_loaded(this)" border="0"></a>',
'<a href="$1">$1</a>',
'<a href="$1" target="_blank">$2</a>',
'<div style="text-align: $1;">$2</div>',
'<a href="mailto:$1">$1</a>',
'<a href="mailto:$1">$2</a>',
'<span style="font-family: $1;">$2</span>',
'<span style="font-size: $1;">$2</span>',
'<span style="color: $1;">$2</span>',
'<pre class="code">$1</pre>',
'<hr/>',
'<br/>'
);
//Do BBCode's
$text = preg_replace($simple_search, $simple_replace, $text);
$text = bbcode_quote($text);
//Do smilies
$text = do_smilies($text);
return $text;
}
//BBCode for Quote Nesting
function bbcode_quote($str) {
$open = '<blockquote>';
$close = '</blockquote>';
//How often is the open tag?
preg_match_all ('/\/i', $str, $matches);
$opentags = count($matches['0']);
//How often is the close tag?
preg_match_all ('/\[\/quote\]/i', $str, $matches);
$closetags = count($matches['0']);
// Check how many tags have been unclosed
// And add the unclosing tag at the end of the message
$unclosed = $opentags - $closetags;
for ($i = 0; $i < $unclosed; $i++) {
$str .= '</blockquote>';
}
// Do replacement
$str = str_replace ('[quote]', $open, $str);
$str = str_replace ('', $close, $str);
return $str;
}
//Function to do smilies
function do_smilies($text){
$new_text = $text;
$smile_diretory = 'images/smilies';
$smilies = glob("$smile_diretory/{*.jpg,*.jpeg,*.gif,*.png}", GLOB_BRACE);
//Round up smiles, their names and atttibutes
foreach($smilies as $smile){
//Smilie Name
$temp = explode('/',$smile);
$smile_name = $temp[count($temp)-1];
//Without the extension
$temp = explode('.',$smile_name);
$new_text = str_replace(':'.$temp[0].':','<img src="'.$smile_diretory.'/'.$smile_name.'" alt="'.$temp[0].'">',$new_text);
}
return $new_text;
}
Peace,
foobic
12-20-2006, 07:59 PM
Also, \1 is equivalent to $1, and it becomes \\1 inside double quotes since you have to escape the \.
I think the originals would mostly work but azizny's are more readable.
SNOWmanx
12-20-2006, 10:25 PM
Would there be a way to modify $1 before we actually print it?
foobic
12-20-2006, 11:02 PM
How do you want to modify it? You'll probably need to change the regexp to find a smaller match that you don't want to modify.
SNOWmanx
12-21-2006, 12:02 AM
I want to perform a closest-match function on the link.