SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Location
    Tacoma, WA
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    replace/parse words with php

    Hi,

    I have a question on how to do this in php.

    For example, in a textbox, there is:

    [RSHIFT]sitepp[BCK]oint is Very[ENTER]Nice
    what i want to do is search for [RSHIFT] and make it so that it'll capitalize the letter after that, in this case, 'S'. And when there's a [BCK], make it send a backspace keystroke so that the character before it will be erased, and when there's a [ENTER], make it go to a new line, so the result would look something like this:

    Sitepoint is Very
    Nice

    If anyone can help me, i'd be thankful.

    Thanks
    TheNewsFeeds.com - Feed your RSS Needs
    IPThing.com - Make longggggg URLs short!

  2. #2
    You talkin to me? Anarchos's Avatar
    Join Date
    Oct 2000
    Location
    Austin, TX
    Posts
    1,438
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know how to do the uppercase part, but for the rest do:
    PHP Code:
    $string preg_replace("/.\[BCK\]/","",$string);
    $string preg_replace("/\[ENTER\]/","<br />\n",$string); 

  3. #3
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Don't forget to escape your brackets, I think Anarchos fell trap to the evil vb and its mysterious missing escape chacaters. I am pretty sure he meant this:
    $string = preg_replace("/.\[BCK\]/","",$string);
    $string = preg_replace("/\[ENTER\]/","<br />\n",$string);


    To do the uppercase trick.

    $string = preg_replace("/\[RSHIFT\](.)/e","strtoupper($1)",$string);
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  4. #4
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Freddy I thought for your efficiency drive you would have suggested

    $string=str_replace("[ENTER]","<br>\n",$string);

    for the newline thingy.

  5. #5
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well yes, although its the only one out of the three that would be able to use str_replace(), but out of all honesty I just fixed the small bug with anarchos' code and added my own for the uppercase part. If I was writing this code, I would probably go back and change it to str_replace() after some fine chap just reminded on how something can be done better. Thanks
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  6. #6
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Location
    Tacoma, WA
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    uppercase

    the code to make it uppercase doesnt seem to work correctly
    TheNewsFeeds.com - Feed your RSS Needs
    IPThing.com - Make longggggg URLs short!

  7. #7
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What version of php are you using? This one is for PHP 4.0.4 and above try this for lower versions:

    $string = preg_replace("/\[RSHIFT\](.)/e","strtoupper(\\1)",$string);
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  8. #8
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Location
    Tacoma, WA
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    php version/ errors

    when i use phpinfo() , i get:
    PHP Version 4.0.4pl1

    and i get
    Parse error: parse error, expecting `')'' in C:\LocalHost\decode.PHP(9) : regexp code on line 1
    and here's my code:
    <?php
    //echo phpinfo();

    if($submit=="Decode"){
    //$string = $txtmsg;
    //$txtmsg = preg_replace("/\[RSHIFT\](.)/e","strtoupper($1)",$txtmsg);
    $txtmsg = preg_replace("/.\[BCK\]/","",$txtmsg);
    $txtmsg = preg_replace("/\[ENTER\]/","<br/>\n",$txtmsg);
    $txtmsg = preg_replace("/\[LSHIFT\](.)/e","strtoupper(\\1)",$txtmsg);

    echo $txtmsg;
    }
    ?>
    TheNewsFeeds.com - Feed your RSS Needs
    IPThing.com - Make longggggg URLs short!

  9. #9
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I will assume that [LSHIFT] is supposed to capitalize the char just to the left of it, if this is the case then you would need to move the (.) to before the [LSHIFT] like

    $txtmsg = preg_replace("/(.)\[LSHIFT\]/e","strtoupper(\\1)",$txtmsg);

    And for [RSHIFT]

    $txtmsg = preg_replace("/\[RSHIFT\](.)/e","strtoupper(\\1)",$txtmsg);
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  10. #10
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Location
    Tacoma, WA
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    hmm...

    freddy,
    i've tried that, and there's still errors
    here's the code:

    decode.php:
    <?php
    //echo phpinfo();

    if($submit=="Decode"){
    $txtmsg = preg_replace("/.\[BCK\]/","",$txtmsg);
    $txtmsg = preg_replace("/\[ENTER\]/","<br/>\n",$txtmsg);
    $txtmsg = preg_replace("/(.)\[LSHIFT\]/e","strtoupper(\\1)",$txtmsg);

    echo $txtmsg;
    }
    ?>
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body bgcolor="#FFFFFF" text="#000000">
    <form method="post" action="decode.php">
    <p>
    <textarea name="txtmsg" cols="90" rows="17"></textarea>
    </p>
    <p>
    <input type="submit" name="submit" value="Decode">
    </p>
    </form>
    </body>
    </html>
    and here's the sample text to decode:
    [LSHIFT]hee[BCK]llo freddy
    I always get the same error for some reason.

    Hope you can help me out.

    Thanks
    TheNewsFeeds.com - Feed your RSS Needs
    IPThing.com - Make longggggg URLs short!

  11. #11
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Again [LSHIFT] is looking for a char to its left, there isn't one so you get an error. Use this it works for me.

    <?php
    //echo phpinfo();

    if($submit=="Decode"){
    $txtmsg = preg_replace("/.\[BCK\]/","",$txtmsg);
    $txtmsg = preg_replace("/\[ENTER\]/","<br/>\n",$txtmsg);
    $txtmsg = preg_replace("/(.)\[LSHIFT\]/e","strtoupper(\\1)",$txtmsg);
    $txtmsg = preg_replace("/\[RSHIFT\](.)/e","strtoupper(\\1)",$txtmsg);

    echo $txtmsg;
    }
    ?>
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body bgcolor="#FFFFFF" text="#000000">
    <form method="post" action="<?=$PHP_SELF?>">
    <p>
    <textarea name="txtmsg" cols="90" rows="17"></textarea>
    </p>
    <p>
    <input type="submit" name="submit" value="Decode">
    </p>
    </form>
    </body>
    </html>


    Example

    [RSHIFT]sitepointt[BCK] is pretty cool[LSHIFT][ENTER][RSHIFT]freddy is king!

    //prints

    Sitepoint is pretty cooL
    Freddy is king!
    Last edited by freddydoesphp; Jul 4, 2001 at 13:05.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  12. #12
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just changed the above code so make sure to copy and paste it now.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  13. #13
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Location
    Tacoma, WA
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    yes!!!

    Hail to king freddy!!!!


    Thanks a lot
    TheNewsFeeds.com - Feed your RSS Needs
    IPThing.com - Make longggggg URLs short!


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
  •