The forums have permanently moved to This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.

Page 1 of 2 12 LastLast
Results 1 to 15 of 24

Thread: PHP -> STRTR() function problem....

  1. #1

    PHP -> STRTR() function problem....

    I'm making a function to replace in a textbox: [b] for <b> ... and that kind of thing... [br] for <br>... etc, so somaone can write smilefaces and then I can replace that by a image [:-)] =>

    my function is:

    PHP Code:
    function colorearTexto($str){
    $RESERVED=array("[b]"=>"<b>","[/b]"=>"</b>","\n"=>"<br>"); //... etc
    return strtr($str$RESERVED);

    My problem is... there is a way to count the number of "[" (in this case) so if they don't match with the numer of "]" an error sintaxis appears... bicouse if someone write:

    hello [b]my name is [/b
    ... etc etc blabla

    and forgot the "]", i don't want that all the text appears in bold...

    or there is another way??

  2. #2
    there is a function to make something like this?

    example: replace(" * ","<b> * </b>", $str); //being the * anything between the tags... ????????????

  3. #3
    your looking for regular expressions.

    i'd recommend googling Regular Expressions... and checking out

    that should help you out
    got pwnt?

  4. #4

  5. #5
    one last question, this function eliminate the "<script>" tags... but, how do I do to replace the "<script>...</script>" for "[script]...[/script]"

    PHP Code:
    $busqueda = array ('@<script[^>]*?>.*?</script>@si');                    
    $reemplazar = array ('');
    $texto preg_replace($busqueda$reemplazar$documento); 
    ... I tell you this bicouse I don't know what this means: '@<script[^>]*?>.*?</script>@si'

  6. #6
    What are you trying to do?

    K-Emmys-06: Best Footer; and K-Emmys-06: Most Active Member

  7. #7
    I 'm trying to make something like the Quick Reply feature of this forum... where I use tags [...] and are replaced by something... like the [_QUOTE_] tags (without the _ )

    so I can make a function that take this :[-b]Bold[-/b] and return < b > Bold < / b >

  8. #8
    PHP Code:
    preg_replace('/\[b\](.*)\[/b]/i''/<b>{1}</b>/, $content); 

    K-Emmys-06: Best Footer; and K-Emmys-06: Most Active Member

  9. #9
    Thanks... that work! las question... what are all that simbols in the first arguments of the function preg_replace?? '/\[b\](.*)\[/b]/i' ??

    whats the diference between:

    preg_replace('/\[b\](.*)\[/b]/i', '/<b>${1}</b>/', $content);


    ('![b\](.*)[/b]!', '<b>${1}</b>', $content);
    Last edited by bandinopla; August 12th, 2005 at 02:40 PM.

  10. #10
    They're probably about equivalent.

    /: start the regular expression (these must enclose the entire expression)
    \: Escape the next character. It makes it so the next character doesn't mean anything.
    [: the start of the pseudo-tag
    b: the bold character
    \: escape the meaning of the next character
    ]: the end of the pseudo-tag
    (: start the area to remember, defined as a variable when replacing
    .: any character
    *: 0-Infinte number of the previous character
    ): close the variable
    \: escape the next character
    [: start pseudo-code
    /: end tag slash
    b: end tag b
    \: escape next character (although not necessary, recommended). I forgot it in my last post.
    ]: end closing tag
    /: end regular expression
    i: make the regular expression case-insensitive
    Last edited by Jeff Wheeler; August 12th, 2005 at 02:48 PM.

    K-Emmys-06: Best Footer; and K-Emmys-06: Most Active Member

  11. #11
    uuuooouu!!!! YEEEAA!!!! Thats what I talking about!!! SUper THANKS nokrev


  12. #12

    K-Emmys-06: Best Footer; and K-Emmys-06: Most Active Member

  13. #13
    PHP Code:
    //$documento = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">';
    //$documento = '<html xmlns="" lang="en" xml:lang="en">';
    //$documento = ' <head>';
    //$documento = '  <title> </title>';
    //$documento = '  <script type="text/javascript" src="js.js"></script>';
    //$documento = ' </head>';
    //$documento = ' <body onload="myDate()">';
    //$documento = '  <h1 class="top">Title</h1>';
    $documento '  <b>Some info</b>';

    $busqueda '/<([\/\!])?([\w]{1,})([^>]*)?>/i';

    //$reemplazar = '[$1$2$3]';  
    $reemplazar '[$1$2]';

    $texto preg_replace($busqueda$reemplazar$documento);

    '<pre>' $texto '</pre>';

    Here's a working example that will work on any HTML tag. All the $documento's at the top are there for you to text and use.

    There are two $reemplazar's there the one that's commented (with $1$2$3) would include everything that's included with the tag (id, classes, other HTML tag attributes).

    So...if you used:

    $documento = '  <h1 class="top">Title</h1>';
    You would see:

    // When $reemplazar = '[$1$2$3]';  
    // You'd see:
    [h1 class="top"]Title[/h1]
    // When $reemplazar = '[$1$2]';
    // You'd see:
    It should work on any HTML tag you need.

  14. #14


    K-Emmys-06: Best Footer; and K-Emmys-06: Most Active Member

  15. #15
    Thanks Ankou!!

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About Meet the Moderators Advertise

 Link to Us


Copyright 1999 - 2012