-
preg_replace
I'm created a bb system and I want the board owner to be able to specify custom bbcode, such as:
Code: @
Replacement: <a href=\\1>\\1</a>
so I have to be able to parse their input into preg strings.
Here is an example of what I have so far:
Example: http://www.vbshelf.com/bb/test.php
Code: http://www.vbshelf.com/bb/test.phps
However, it doesn't quite work. Does anybody have any ideas why?
-
PHP Code:
function ParseCode($text = "Invalid use of Function",$abCode = 1) {
if ($abCode) {
$abArray = array();
$htmlArray = array();
$query = mysql_query("select * from abCode");
for($i=0;$i<mysql_numrows($query);$i++) {
$abTag = mysql_result($query,$i,"abTag");
$htmlTag = mysql_result($query,$i,"htmlTag");
$option = 1;
while (preg_match("/\\{(option|param)\\}/is",$abTag)) {
$abTag = preg_replace("/\\{(option|param)\\}/is","(.*?)",$abTag,1);
$option++;
}
$abArray[$i] = $abTag;
$replace = 1;
while (preg_match("/\\{(option|param)\\}/is",$htmlTag)) {
$htmlTag = preg_replace("/\\{(option|param)\\}/is","\\\\\\\\".$replace,$htmlTag,1);
$replace++;
}
$htmlArray[$i] = $htmlTag;
}
for ($i=0;$i<sizeof($abArray);$i++) {
$abArray[$i] = str_replace("[","\\[",$abArray[$i]);
$abArray[$i] = str_replace("]","\\]",$abArray[$i]);
$abArray[$i] = str_replace("/","\\/",$abArray[$i]);
$text = preg_replace("/".$abArray[$i]."/",$htmlArray[$i],$text);
}
return $text;
}
}
This is what I use in my Bloggernaut Script. All it looks for is the phrases "{option}" and "{param}" (you can find this function plus the function that takes the user input in the functions.php file in my bloggernaut script) and replaces those with the correct \\1 or \\2 accordingly.
When you have the user input his/her tag all you need to check is that the HTML version has the same number of {option} and {param} tags as the bbCode field.
-Matt
-
so you don't know why mine's not working?
It does everything right, but when it displays it just shows \1 instead of the text. and it's bold like it's supposed to be.
I know it has to be something simple.
-
BTW, Matt, when I tried to download Bloggernaut, nothing happened. No download page, no e-mail...nothing.
I put in my correct e-mail and everything.
-
Nevermind, I have it working now.
-
cool :D
I had to take down the download page on Bloggernaut so Ican focus on some legal issues that involve it, but if I had seen your code I would probably have guessed it dealt with the number of backward slashes you were using. You have to use 4, so PHP recognizes it as 2. ;)
-
I changed it so it "autodetects" where the numbers should be so the user doesn't have to. It's working pretty good now. :)