|
-
Jun 7th, 2002, 06:01 AM
#1
Thread Starter
Fanatic Member
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.
Last edited by cpradio; Jun 7th, 2002 at 08:43 AM.
-
Jun 7th, 2002, 08:39 AM
#2
Thread Starter
Fanatic Member
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
Last edited by cpradio; Jun 7th, 2002 at 08:44 AM.
-
Jun 7th, 2002, 10:50 AM
#3
Thread Starter
Fanatic Member
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;
}
}
-
Jun 7th, 2002, 10:59 AM
#4
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 ]
-
Jun 7th, 2002, 11:54 AM
#5
Stuck in the 80s
I've always been confused by preg_replace and ereg_replace. I just use str_replace. Sounds cooler to me.
-
Jun 7th, 2002, 11:57 AM
#6
Thread Starter
Fanatic Member
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.
-
Jun 7th, 2002, 12:44 PM
#7
Stuck in the 80s
I don't see why you'd have to know what was between the tags?
-
Jun 7th, 2002, 12:49 PM
#8
Thread Starter
Fanatic Member
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.
-
Jun 7th, 2002, 01:29 PM
#9
Thread Starter
Fanatic Member
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
-
Jun 7th, 2002, 03:26 PM
#10
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|