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

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

  1. #1

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

    Hi,
    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 www.php.net/preg_replace

    that should help you out
    got pwnt?

  4. #4
    YYYYYYYYEEEEEEEEEEEAAAAAAAAAAAAAAAA!!!!!!!!!!!!!!! ! THAnkS .harish YAEEAAAAAAAAAAA!!!!!

  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);

    and


    preg_replace
    ('![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

    yea!!

  12. #12
    np


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

  13. #13
    PHP Code:
    <?php
    //$documento = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
    //$documento = '<html xmlns="http://www.w3.org/1999/xhtml" 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);

    echo 
    '<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:

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

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

  14. #14


    Show-off


    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 kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012