SitePoint Sponsor

User Tag List

Results 1 to 8 of 8

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Advanced str_replace()

    I was wondering if anyone could help out.
    I want to create a script which acts in a way similar to BBcode.
    In a user-editable text-box, users would be able to write thinks like:

    [link url="http://www.sitepoint.com"]Go To Sitepoint[/link]
    I want to change this into the form of:

    Code HTML4Strict:
    <a href="http://www.sitepoint.com">Go To Sitepoint</a>

    I think PHP's str_replace() function would be the correct method, but I just can't figure out how I would use is.
    I know that I could just use BBcode but i would like to be able to make this from scratch, as it needs some unique features.
    Any help on how I would do this would be appreciated.

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    preg_replace would be better for this.
    Here is a function I use:
    PHP Code:
    function bbcode_format($str) {

            
            
    $str htmlentities($str); 

            
    $simple_search = array(
                                    
    '/\[h1\](.*?)\[\/h1\]/is',
                                    
    '/\[h2\](.*?)\[\/h2\]/is',
                                    
    '/\[h3\](.*?)\[\/h3\]/is',
                                    
    '/\[h4\](.*?)\[\/h4\]/is',
                                    
    '/\[b\](.*?)\[\/b\]/is',                                
                                    
    '/\[i\](.*?)\[\/i\]/is',                                
                                    
    '/\[u\](.*?)\[\/u\]/is',
                                    
    '/\[s\](.*?)\[\/s\]/is',
                                    
    '/\[LINK=(.*?)\](.*?)\[\/LINK]/is',
                                    
    '/\[red\](.*?)\[\/red]/is',
                                    
    '/\[blue\](.*?)\[\/blue]/is',
                                    
    '/\[IMG\](.*?)\[\/IMG\]/is',
                                    );

            
    $simple_replace = array(
                                    
    '<h1>$1</h1>',
                                    
    '<h2>$1</h2>',
                                    
    '<h3>$1</h3>',
                                    
    '<h4>$1</h4>',
                                    
    '<strong>$1</strong>',
                                    
    '<em>$1</em>',
                                    
    '<u>$1</u>',
                                    
    '<s>$1</s>',
                                    
    '<a href="$1">$2</a>',
                                    
    '<span style="color: red">$1</span>',
                                    
    '<span style="color: blue">$1</span>',
                                    
    '<div class="img_border"><img src="'constant('IMAGE_PATH') .'/thumbs/$1" alt="$1"/></div>',
            
                                    );

            
    // Do simple BBCode's
            
    $str preg_replace ($simple_search$simple_replace$str);

            return 
    $str;


    usage
    PHP Code:
    $txt "this is [link="http://www.sitepoint.com"]Go To Sitepoint[/link]
    echo bbcode_format($txt); 
    Try it and see what happens
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thats great, and it worked fine. Thanks!
    Do you think you could explain the elements inside the arrays in the function? That would help me a lot. I get how it works, im just unsure of what some of the values in the arrays mean.
    Thanks again.

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    Sure

    PHP Code:
    $str htmlentities($str); 
    http://www.php.net/htmlentites
    (saves me saying it!)

    What you then have are 2 arrays; the first holds the pattern for replacement and the second is the pattern to search for.

    So to take the H1 as an example
    PHP Code:
    '/\[h1\](.*?)\[\/h1\]/is'

    / open the pattern
    \ escapes the first [
    h1 is then obviously the tag
    \ escapes the second ]
    (.*?) is an ATOM and basically a greedy one. It says match anything between the [h1]..[/h1] tags
    \[ escapes the [
    \/ escapes the /
    h1 other tag
    \] escapes ]
    / closes the pattern
    i case insensitive search
    s any whitespace character

    This then stores the matches it finds in the $1 variable ($1 because there is one atom)

    the replacement '<h1>$1</h1>' takes the contents of $1 and puts it between the <h1></h1> tags.

    HTH and welcome to the forums
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    SitePoint Guru
    Join Date
    Jul 2005
    Location
    Orlando
    Posts
    634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spikeZ View Post
    S
    (.*?) is an ATOM and basically a greedy one. It says match anything between the [h1]..[/h1] tags
    Just as a point of clarification, this is NOT a greedy regex. The ? at the end specifically tells it not to be greedy.

  6. #6
    SitePoint Member
    Join Date
    Apr 2007
    Posts
    14
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spikeZ View Post
    Sure

    PHP Code:
    $str htmlentities($str); 
    http://www.php.net/htmlentites
    (saves me saying it!)

    What you then have are 2 arrays; the first holds the pattern for replacement and the second is the pattern to search for.

    So to take the H1 as an example
    PHP Code:
    '/\[h1\](.*?)\[\/h1\]/is'

    / open the pattern
    \ escapes the first [
    h1 is then obviously the tag
    \ escapes the second ]
    (.*?) is an ATOM and basically a greedy one. It says match anything between the [h1]..[/h1] tags
    \[ escapes the [
    \/ escapes the /
    h1 other tag
    \] escapes ]
    / closes the pattern
    i case insensitive search
    s any whitespace character

    This then stores the matches it finds in the $1 variable ($1 because there is one atom)

    the replacement '<h1>$1</h1>' takes the contents of $1 and puts it between the <h1></h1> tags.

    HTH and welcome to the forums
    Really, really useful! Thanks, that cleared up a lot. If this forum had reputation or something (which it should) you would be getting some. Thanks again.

  7. #7
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    good spot UFTimmy
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  8. #8
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,806
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    no problem, glad I could help
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


Bookmarks

Posting Permissions

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