Results 1 to 10 of 10

Thread: preg_replace

  1. #1

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616

    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...
    &nbsp;&nbsp;$abOpenTag has the value of "[ b ]" (without the spaces inbetween)
    &nbsp;&nbsp;$abCloseTag has the value of "[ /b ]" (without the spaces inbetween)
    &nbsp;&nbsp;$htmlOpenTag has the value of "<b>"
    &nbsp;&nbsp;$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.
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  2. #2

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    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:
    &nbsp;&nbsp;$abOpenTag has the value of "b"
    &nbsp;&nbsp;$abCloseTag has the value of "/b"
    &nbsp;&nbsp;$htmlOpenTag has the value of "<b>"
    &nbsp;&nbsp;$htmlCloseTag has the value of "</b>"

    -Matt
    Last edited by cpradio; Jun 7th, 2002 at 08:44 AM.
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  3. #3

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616

    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;
        }

    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  4. #4
    scoutt
    Guest
    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 ]

  5. #5
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I've always been confused by preg_replace and ereg_replace. I just use str_replace. Sounds cooler to me.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    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.
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  7. #7
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I don't see why you'd have to know what was between the tags?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  8. #8

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    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.
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  9. #9

    Thread Starter
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    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
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  10. #10
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Gotcha
    My evil laugh has a squeak in it.

    kristopherwilson.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width