Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jan 2010
    Posts
    101
    Thanks
    17
    Thanked 5 Times in 5 Posts

    Regex replacing custom tags..

    Hey everyone. I need some help with a regex function in my script.

    I'm trying to replace content like: something {tag}hello{/tag} something else

    The idea is that there can be anything surrounding those tags, and anything inside.

    Using preg_replace, I want to remove the tags and the contents of the tags, but keep the outside stuff, and replace what used to be in the tags position.

    Any help greatly appreciated!

  • #2
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    PHP Code:
    function tag_replace($matches) {
        echo 
    '<pre>'.print_r($matchestrue).'</pre>';
    }

    $regex '%\{([^}]+)\}(.*?)\{/\1\}%';
    $text 'something {tag}hello{/tag} something else';
    preg_replace_callback($regex'tag_replace'$text); 
    In your function called tag_replace, $matches[1] will contain the tag type (tag in this example) and $matches[2] will contain the content (in this case hello)

    To alter the text, simply return the text you wish to replace the tags

    Here's an example of how to make an "img" tag
    PHP Code:
    function tag_replace($matches) {
        switch(
    $matches[1]) {
            case 
    'img':
                return 
    '<img src="'.$matches[2].'" alt="Image" />';
                break;
            default:
                return 
    $matches[0];
                break;
        }
    }

    $regex '%\{([^}]+)\}(.*?)\{/\1\}%';
    $text 'something {img}http://www.abc.com/img.jpg{/img} something else';
    echo 
    preg_replace_callback($regex'tag_replace'$text); 
    Will produce
    something <img src="http://www.abc.com/img.jpg" alt="Image" /> something else
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • Users who have thanked JAY6390 for this post:

    iLochie (02-24-2010)

  • #3
    Regular Coder
    Join Date
    Jan 2010
    Posts
    101
    Thanks
    17
    Thanked 5 Times in 5 Posts
    Thanks Jay, but the way I was using before worked after I changed a few things, here's my regex. It's nothing special but it will get the job done for what I need:

    PHP Code:
    $page preg_replace("/(.*?)".$block[0]."(.*?)".$block[1]."(.*?)/is","\\1".$block_new."\\3",$page); 
    Where $block[0] is {tag} and $block[1] is {/tag} and $block_new replaces the code between $block[0] and $block[1].

  • #4
    Regular Coder cyrus709's Avatar
    Join Date
    Dec 2008
    Posts
    102
    Thanks
    22
    Thanked 5 Times in 5 Posts
    Here is an example of some code that i wrote for adding BB codes to a profile.

    Note that the profile text is submitted on another page and recieved on this page, then redirected to the profile.
    edit_profile_page < php_code < profile_page

    PHP Code:
    <?php
    //session 
             
    session_start(); 
          if(
    $_SESSION['logged'] != 1){ header("location:index.html"); } 
          
    $convert$_SESSION['username'];
    //connects to mysql
    $con mysql_connect("localhost""root""");
    if(!
    $con)
    {
    die();
    }
    //selects DB
    mysql_select_db("text"$con);
    //Gets text
    $text $_POST['profiletxt'];
    /*strips html tags */
    $text strip_tags($text);
    //text can not be longer then 45 words
    $text wordwrap($text45"<br>");


    /*specifies and replaces BB codes with appropriate html
    currently all bb codes are here, some need to be taken out */

    $text preg_replace ('/\[font color="([a-zA-Z0-9\#]+)"\]/' '<font color="\\1">' $text ); 
    $text preg_replace ('/\[font size="([0-9]+)"\]/' '<font size="\\1">' $text ); 
    $replace = array("[b]""[/b]""[/font]""[graph]""[/graph]""[i]""[/i]""[u]""[/u]""[img]""[/img]");
    $BBC   = array("<b>""</b>""</font>""<img src=http://localhost/Global%20C/profilegraph.php?username="">""<i>""</i>""<u>""</u>""<img src=\"""\">");

    $text str_replace($replace$BBC$text);
    print 
    $text;

    mysql_query("UPDATE users SET Profile = '$text' WHERE username = '$convert'");
    header("location: profile.php?username=" $convert);
    ?>
    In summary this script strips all html tags from the text, searches and replaces bb codes with proper html tags. This code obviously can not be copied and pasted, but can definitely be edited or at the least an example of how i would have done it
    Last night I lay in bed looking up at the stars in the sky and I thought to myself, where the heck is the ceiling.


  •  

    Posting Permissions

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