SitePoint Sponsor

User Tag List

Results 1 to 17 of 17

Hybrid View

  1. #1
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    str_replace(); and other problems.

    When I use the following code:
    Code:
    <?php
        // site template
        include("header.php");
        include("toppage.php");
        // display the information to be previewed
        $oldcontent = $updatecontent;
        $oldcontent = eregi_replace("\[i\]","<span class=\"italic\">",$oldcontent);
        $oldcontent = eregi_replace("\[/i\]","</span>",$oldcontent);
        $oldcontent = eregi_replace("\[b\]","<span class=\"bold\">",$oldcontent);
        $oldcontent = eregi_replace("\[/b\]","</span>",$oldcontent);
        $oldcontent = eregi_replace("\[u\]","<span class=\"underline\">",$oldcontent);
        $oldcontent = eregi_replace("\[/u\]","</span>",$oldcontent);
        $oldcontent = eregi_replace("\[list\]","<ul>",$oldcontent);
        $oldcontent = eregi_replace("\[/list\]","</ul>",$oldcontent);
        $oldcontent = eregi_replace("\[\*\]","<li>",$oldcontent);
        $oldcontent = eregi_replace("\[/\*\]","</li>",$oldcontent);
        $oldcontent = eregi_replace("\[space/\]","&nbsp;",$oldcontent);
        $oldcontent = str_replace('\"','"',$oldcontent);
        echo($oldcontent);
        $newcontent = "<br />\n<br />\n
        <form action='$PHP_SELF' method=\"post\">\n
        <input type=\"hidden\" name=\"updateid\" value='$updateid' />\n<br />\n
        <input type=\"hidden\" name=\"updatedate\" value='$updatedate' />\n
        <p>$updatedate</p>\n
        <table width=\"670\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n<tr>\n
        <td width=\"200\" align=\"left\" valign=\"top\"><p>Title:</p></td>\n
        <td width=\"470\" align=\"center\" valign=\"top\"><input type=\"text\" name=\"updatetitle\" size=\"63\" value='$updatetitle' /></td>\n</tr>\n<tr>\n
        <td width=\"670\" colspan=\"2\" align=\"left\" valign=\"top\"><p>Content:</p></td>\n</tr>\n<tr>\n
        <td width=\"670\" colspan=\"2\" align=\"center\" valign=\"top\"><textarea name=\"updatecontent\" cols=\"80\" rows=\"10\" wrap>$updatecontent</textarea></td>\n</tr>\n<tr>\n
        <td width=\"670\" colspan=\"2\" align=\"left\" valign=\"top\"><p>&nbsp;</p></td>\n</tr>\n<tr>\n
        <td width=\"670\" colspan=\"2\" align=\"center\" valign=\"top\"><input type=\"submit\" value=\"Update!\" name=\"submit\" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input type=\"submit\" value=\"Preview\" name=\"preview\" /></td>\n</tr>\n
        </table>\n</form>\n<br />\n<br />\n
        <p><a href=\"admin_pages.php\">Back to the Site Page Manager</a><br />\n
        <a href=\"admin.php\">Back to the Administration Center</a></p><br />";
        $newcontent = str_replace('\"','"',$newcontent);
        echo($newcontent);
        include("footer.php");
    ?>
    The str_replace();s don't seem to be working, the returned data instead has an extra \ in the code.

    Any ideas?
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  2. #2
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    $oldcontent = eregi_replace('\"','"',$oldcontent);
    $newcontent = eregi_replace('\"','"',$newcontent);
    That doesn't work either.
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  3. #3
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You don't need to remove those slashes...they're just used to escape the double quotes (")...they won't show up in your output. If for some weird reason they are, just use the stripslashes() function.

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    * Added vb won't display the tags correctly. but I think you get the picture right.


    Optionally all those ereg_replace() can be changed to str_replace, since its just a straight replacement and you don't need the extra functionality for regular expressions. This will speed your script up. Also str_replace() can take arrays, so you can do the whole replacement in one line.
    PHP Code:
    $s = array('\[i\]',
               
    '\[b\]',
               
    '\[list\]',
               
    '\[*\]',
               
    '\[space\]',
               
    '\[/i\]',
               
    '\[/b\]',
               
    '\[/list\]',
               
    '\[/*\]'
               
    );
    $e = array('<span class="italic">',
               
    '<span class="bold">',
               
    '<ul>',
               
    '<li>',
               
    ' ',
               
    '</span>',
               
    '</span>',
               
    '</ul>',
               
    '</li>'
               
    );
    $oldcontent str_replace($s$e$oldcontent); 
    Last edited by freddydoesphp; Sep 23, 2001 at 22:55.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd like to add that, if you do switch to str_replace(), you won't need to escape the brackets ([) anymore. Nice tip, BTW, Freddyt didn't know it accepted arrays. Very cool.

  6. #6
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you both very much! I'll implement those arrays as soon as I can freddy. Thanks for your help as well Chris!

    freddy: Sorry about sending you the e-mail; you can disregard it now.

    ::walks away w/out a care in the world::
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  7. #7
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    All right -- here's the new code so far:
    Code:
    <?php
    {
        // site template
        include("header.php");
        include("toppage.php");
        // display the information to be previewed
        $schwayscriptopen = array('\[i\]','\[b\]','\[list\]','\[\*\]','\[space\]','\[/i\]','\[/b\]','\[/list\]','\[/\*\]');
        $schwayscriptclose = array('<span class="italic">','<span class="bold">','<ul>','<li>',' ','</span>','</span>','</ul>','</li>');
        $newcontent = str_replace($schwayscriptopen,$schwayscriptclose,$updatecontent);
        echo(stripslashes($newcontent));
        $text = "<br />\n<br />\n
        <form action='$PHP_SELF' method=\"post\">\n
        <input type=\"hidden\" name=\"updateid\" value='$updateid' />\n<br />\n
        <input type=\"hidden\" name=\"updatedate\" value='$updatedate' />\n
        <p>$updatedate</p>\n
        <table width=\"670\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">\n<tr>\n
        <td width=\"200\" align=\"left\" valign=\"top\"><p>Title:</p></td>\n
        <td width=\"470\" align=\"center\" valign=\"top\"><input type=\"text\" name=\"updatetitle\" size=\"63\" value='$updatetitle' /></td>\n</tr>\n<tr>\n
        <td width=\"670\" colspan=\"2\" align=\"left\" valign=\"top\"><p>Content:</p></td>\n</tr>\n<tr>\n
        <td width=\"670\" colspan=\"2\" align=\"center\" valign=\"top\"><textarea name=\"updatecontent\" cols=\"80\" rows=\"10\" wrap>$updatecontent</textarea></td>\n</tr>\n<tr>\n
        <td width=\"670\" colspan=\"2\" align=\"left\" valign=\"top\"><p>&nbsp;</p></td>\n</tr>\n<tr>\n
        <td width=\"670\" colspan=\"2\" align=\"center\" valign=\"top\"><input type=\"submit\" value=\"Update!\" name=\"submit\" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <input type=\"submit\" value=\"Preview\" name=\"preview\" /></td>\n</tr>\n
        </table>\n</form>\n<br />\n<br />\n
        <p><a href=\"admin_pages.php\">Back to the Site Page Manager</a><br />\n
        <a href=\"admin.php\">Back to the Administration Center</a></p><br />";
        echo(stripslashes($text));
        include("footer.php");
    }
    ?>
    Everything is working great except for one thing: the str_replace (with the arrays) is not replacing the characters like it is supposed to. Any ideas on this one?
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  8. #8
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Take the backslashes out of the first array, you don't need to escape the brackets ([ and ]).

  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)
    or the asterisks
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  10. #10
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You both are great! Now I have another question (it's not a problem this time though)!

    Why is it that str_replace(); works but if I replace that with eregi_replace(); or ereg_replace(); it doesn't?

    Just curious, so I know what to use for future use. How do I destinguish between them (string, regularexpressions case-insensitive and regularexpressons case-sensitive).
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  11. #11
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use str_replace() for ANYTHING that does not need to use regular expressions. If you are using ereg/egregi, etc, then you'll have to escape, I believe each opening bracket ([) that is meant literally...because brackets are used in regular expression pattern matching.

  12. #12
    Serial Publisher silver trophy aspen's Avatar
    Join Date
    Aug 1999
    Location
    East Lansing, MI USA
    Posts
    12,937
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    in other words if you don't know what a regular expression is or how to write them don't use them.
    Chris Beasley - I publish content and ecommerce sites.
    Featured Article: Free Comprehensive SEO Guide
    My Guide to Building a Successful Website
    My Blog|My Webmaster Forums

  13. #13
    SitePoint Wizard Aes's Avatar
    Join Date
    Jun 2001
    Location
    Oklahoma
    Posts
    3,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I know exactly what they are Chris B., just not when to use those *_replace()s.

    Thanks Chris (TWT) for the explanation.
    Colin Anderson
    Ambition is a poor excuse for those without
    sense enough to be lazy.

  14. #14
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also str_repalce() is the only one that will take arrays as inputs. ereg_replace() will not.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  15. #15
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and, i'm wondering, why does everyone use the ereg() functions all the time?! the preg() functions are twice as fast. and they're closer to Perl so they're better IMO.

    just had to throw that in.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR

  16. #16
    Happy Holidays !! Paul S's Avatar
    Join Date
    Mar 2001
    Location
    Mexico
    Posts
    1,287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by freddydoesphp
    Also str_repalce() is the only one that will take arrays as inputs. ereg_replace() will not.
    preg_replace too!

  17. #17
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent! I need to get a better grasp on those preg functions and PERL regex in general.
    Please don't PM me with questions.
    Use the forums, that is what they are here for.


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
  •