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 6 of 6
  1. #1
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post

    Need Help: str_replace() for UBB code

    Hi, trying to convert a UBB url into an actual URL with the <a> anchor

    text I am trying to convert:

    Code:
    [ u r l ]http://www.website.com[ / u r l ]

    code to convert it into HTML using str_replace would be what exactly?

    search would be: ' [ u r l ] "', " [ / u r l ]", (without the spaces)

    so the replace would be? ...

    thanks

  • #2
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    Well.. I don't know if you can with str_replace, because you'd need to 'catch' the URL, but with regex you could.

    PHP Code:
    <?php

    $string 
    '[url]http://www.url.com.au[/url]';

    echo 
    preg_replace('#\[url\]([^\[]+)\[/url\]#''<a href="$1">$1</a>'$string);
    Come to think of you could could do..
    PHP Code:
    <?php

    $string 
    '[url]http://www.url.com.au[/url]';

    $string str_replace('[url]''<a href="'$string);
    $string str_replace('[/url]''">Link</a>'$string);

    echo 
    $string;
    But that's a really generic way and all links will be 'Link'
    Last edited by Iszak; 03-01-2009 at 12:13 AM.

  • Users who have thanked Iszak for this post:

    Bobafart (03-01-2009)

  • #3
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post
    Hmm.. that doesn't seem to work.. Here is my code:

    Code:
    function bb2html($text)
    
    {
     // first take care of the [ u r l ]http://www.cnn.com[ / u r l ] type of links
    
      preg_replace('#\[ u r l \]([^\[]+)\[/ u r l \]#', '< a h r e f="$1">$1< / a >', $text);
    
      // then take care of all of the other ubb codes
      $bbcode = array(
    "<", ">",
    
                    "[l is t]", "[*]", "[/li s t]", 
    
                    "[i m g]", "[/ i mg]", 
    
                    "[ b ]", "[/ b ]", 
    
                    "[ u ]", "[/ u ]", 
    
                    "[ i ]", "[/ i ]",
    
                    '[c o lor="', "[/c o lor]",
    
                    "[s ize=\"", "[/s ize]",
    				
                   // '[u rl="', "[/u rl]",
    
                  //  '[u rl=', "[/u rl]",    // url with and without the beginning " quote
    
                    "[m ail=\"", "[/m ail]",
    
                    "[c ode]", "[/c ode]",
    
                    "[q uote]", "[/q uote]",
    
                    '"]',
    
                    ']');  // url with and without the beginning " quote
    
      $htmlcode = array(
      //"&lt;", "&gt;",
    
                    "<ul>", "<li>", "</ul>", 
    
                    "<img src=\"", "\">", 
    
                    "<b>", "</b>", 
    
                    "<u>", "</u>", 
    
                    "<i>", "</i>",
    
                    "<span style=\"color:", "</span>",
    
                    "<span style=\"font-size:", "</span>",
    
                  //  '<a href="', "</a>",
    
                 //   '<a href="', "</a>",
    
                    "<a href=\"mailto:", "</a>",
    
                    "<code>", "</code>",
    
                    "<table width=100% bgcolor=lightgray><tr><td bgcolor=white>", "</td></tr></table>",
    
                    '">',
    
                    '">');
    
      $newtext = str_replace($bbcode, $htmlcode, $text);
    
      $newtext = nl2br($newtext);//second pass
    
      return $newtext;
    
    }
    
    
    // $mysql SELECT query code goes here, stored in array using mysql_fetch-array...
    while($row=mysql_fetch_array...){
     ..
     echo 'text: '.bb2html($row[contentBody]) ;
     ..
    }
    An example of the data field stored in mySQL is:

    Code:
    Heres a review of the famous fitness Bender Ball [ u r l ]http://www.domain.-c-o-m/tv-products/bender-ball-review[/ u r l ]
    // once again I added the spaces in the url tag
    The parsed output using the above PHP code is:

    Heres a review of the famous fitness Bender Ball [url">h-t-t-p://w-w-w-.domain.c-o-m/tv-products/bender-ball-review[/url">

    // once again I added the spaces in the url tag
    // I added the hyphens in "http" to prevent an auto parse by the forum
    so i am clearly doing something wrong....
    Last edited by Bobafart; 03-01-2009 at 01:33 AM.

  • #4
    Regular Coder Iszak's Avatar
    Join Date
    Jun 2007
    Location
    Perth, Western Australia
    Posts
    332
    Thanks
    2
    Thanked 58 Times in 57 Posts
    Try this..
    PHP Code:
    preg_replace('#\[url\]([^\[]+)\[/url\]#is''<a href="$1">$1</a>'$text); 
    That should work.
    Last edited by Iszak; 03-01-2009 at 12:38 AM.

  • Users who have thanked Iszak for this post:

    Bobafart (03-01-2009)

  • #5
    Regular Coder
    Join Date
    Dec 2006
    Posts
    417
    Thanks
    168
    Thanked 1 Time in 1 Post
    well that made absolutely no difference

    can anyone help please?

  • #6
    Regular Coder Deacon Frost's Avatar
    Join Date
    Feb 2008
    Location
    Between the Lines
    Posts
    279
    Thanks
    31
    Thanked 4 Times in 4 Posts
    =/.

    PHP Code:
    <?

    // The url
    $url "http://google.com";

    // What you want displayed for the link
    $title "Google";

    // Convert, if you already have this part of the string, just use it
    $newurl "[url]" $url "[/url]";

    // Change the beginning to display
    $part1url str_replace('[url]''<a href="'$newurl);

    // Change the end to display
    $part2url str_replace('[/url]''">' $title '</a>'$newurl);

    // Put the two togeter
    $finalurl $part1url "" $part2url;

    // Prints: "Google" with link
    echo $finalurl;

    ?>
    That's how I would go about it, at least.


  •  

    Posting Permissions

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