Results 1 to 7 of 7

Thread: preg_replace

  1. #1

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256

    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?
    My evil laugh has a squeak in it.

    kristopherwilson.com

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

  3. #3

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  4. #4

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    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.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  5. #5

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Nevermind, I have it working now.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  6. #6
    Fanatic Member cpradio's Avatar
    Join Date
    Apr 2002
    Posts
    616
    cool

    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.
    http://cpradio.net/
    Administrator @ WDForums and a Moderator @ WebXpertz City Forums

  7. #7

    Thread Starter
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    I changed it so it "autodetects" where the numbers should be so the user doesn't have to. It's working pretty good now.
    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