-
preg_replace
PHP Code:
// Parse all blogCode used in this comment/blog
// $contents = preg_replace("/\\[b\\](.?*)\\[\\/b]\\]/is","<b>$1</b>",$contents);
function ParseCode($text = "Invalid use of Function",$abCode = 1) {
if ($abCode) {
$query = mysql_query("select * from abCode");
for ($i = 0; $i < mysql_numrows($query); $i++) {
$abOpenTag = mysql_result($query,$i,"abOpenTag");
$abCloseTag = mysql_result($query,$i,"abCloseTag");
$htmlOpenTag = mysql_result($query,$i,"htmlOpenTag");
$htmlCloseTag = mysql_result($query,$i,"htmlCloseTag");
$text = preg_replace("/".addslashes($abOpenTag)."(.?*)".addslahses($abCloseTag)."/is","$htmlOpenTag$1$htmlCloseTag",$text);
}
return $text;
}
}
This is my first experiment with preg_replace and I want to know if this will give me the desired effect if...
$abOpenTag has the value of "[ b ]" (without the spaces inbetween)
$abCloseTag has the value of "[ /b ]" (without the spaces inbetween)
$htmlOpenTag has the value of "<b>"
$htmlCloseTag has the value of "</b>"
Will that work, or do I need to change a few things? I tried installing IIS yesterday but had all kinds on my laptop (i think its too slow :() but if anyone could help me here, I would appreciate it.
-
Would it be better to only allow them to enter in the text that goes between the [ and ] or should I allow them to write the entire tag?
Here is the code I think will work if I just allow them to enter in the text.
PHP Code:
// Parse all blogCode used in this comment/blog
// $contents = preg_replace("/\\[b\\](.?*)\\[\\/b\\]/is","<b>$1</b>",$contents);
function ParseCode($text = "Invalid use of Function",$abCode = 1) {
if ($abCode) {
$query = mysql_query("select * from abCode");
for ($i = 0; $i < mysql_numrows($query); $i++) {
$abOpenTag = mysql_result($query,$i,"abOpenTag");
$abCloseTag = mysql_result($query,$i,"abCloseTag");
$htmlOpenTag = mysql_result($query,$i,"htmlOpenTag");
$htmlCloseTag = mysql_result($query,$i,"htmlCloseTag");
$text = preg_replace("/\\[$abOpenTag\\](.?*)\\[\\/$abCloseTag\\])/is","$htmlOpenTag$1$htmlCloseTag",$text);
}
return $text;
}
}
Then let's say the following was entered:
$abOpenTag has the value of "b"
$abCloseTag has the value of "/b"
$htmlOpenTag has the value of "<b>"
$htmlCloseTag has the value of "</b>"
-Matt
-
Nevermind I solved it.
PHP Code:
// Parse all blogCode used in this comment/blog
// $contents = preg_replace("/\\[b\\](.*?)\\[\\/b\\]/is","<b>$1</b>",$contents);
function ParseCode($text = "Invalid use of Function",$abCode = 1) {
if ($abCode) {
$query = mysql_query("select * from abCode");
for ($i = 0; $i < mysql_numrows($query); $i++) {
$abTag = mysql_result($query,$i,"abTag");
$htmlOpenTag = mysql_result($query,$i,"htmlOpenTag");
$htmlCloseTag = mysql_result($query,$i,"htmlCloseTag");
$text = preg_replace("/\\[$abTag\\](.*?)\\[\\/$abTag\\]/is",
"$htmlOpenTag$1$htmlCloseTag",$text);
}
return $text;
}
}
-
I'm not sure what you are trying to get out of this.
it looks like you are replacing [b] with this <b></b> and then same with the closing [ /b ]
-
I've always been confused by preg_replace and ereg_replace. I just use str_replace. Sounds cooler to me.
-
im writing my own vbCode except all tags will be stored in a database and users will be able to define new tags.
And I must use preg_replace as it will allow me to figure out the text inbetween the tags much easier than i can with str_replace.
-
I don't see why you'd have to know what was between the tags?
-
later I will because it may contain special attributes I need to parse and whatnot.
Also the reason behind this is so I can allow people to customize how comments and news are shown using the following:
{title}, {author}, {date}, {post}, {comment}, etc.
-
Also I do not want all the [ and ] changed to < and > just certain ones that pertain to the tags in the database. So preg_replace helps there too.
-Matt
-