SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    str_replace in a function

    Anybody have any clue how to get this to work? If I do a manual str_replace, it works but when I try to do it as a function, it does not. I want to do it as a function b/c I want to add link support to the function as well. Anyways, here's the code (and the query using strings DOES work ):
    PHP Code:
    $code=array("\n",
        
    "/r",
        
    "/n/r",
        
    '[ i]',
        
    '[ /i]',
        
    '[ b]',
        
    '[ /b]',
        
    '[ u]',
        
    '[ /u]',
        
    '[ list]',
        
    '[ *]',
        
    '[ /*]',
        
    '[ /list]');
    $html=array("<br />",
        
    "<br />",
        
    "</p>\n</p>",
        
    '<span style="font-style : italic ;">',
        
    '</span>',
        
    '<span style="font-weight : bold ;">',
        
    '</span>',
        
    '<span style="text-decoration : underline ;">',
        
    '</span>',
        
    '<ul>',
        
    '<li>',
        
    '</li>',
        
    '</ul>');
    function 
    tohtml($message)
    {
        
    $output=str_replace($code,$html,$message);
        return 
    $output;
    }
    function 
    fromhtml($message)
    {
        
    $output=str_replace($html,$code,$message);
        return 
    $output;
    }

    $query "SELECT * FROM $table WHERE $field = '$page'";
    $qyres mysql_query($query);
    while(
    $row=mysql_fetch_array($qyres)) {
        if(!isset(
    $sid)) {
            
    $pid=$row['pageID'];
            
    $pname=$row['pagename'];
            
    $sppage=$row['specialpage'];
            
    $content=tohtml($row['content']);
            }

    yada.... 
    Last edited by Sketch; Feb 22, 2002 at 00:22.
    Aaron Brazell
    Technosailor



  2. #2
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Looks like you just need to register $code and $html as global variables within the functions;

    Code:
    function tohtml($message)
    {
        global $code, $html;
        $output=str_replace($code,$html,$message);
        return $output;
    }
    function fromhtml($message)
    {
        global $code, $html;
        $output=str_replace($html,$code,$message);
        return $output;
    }

  3. #3
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sweet! You, my friend are the man!

    Sketch
    Aaron Brazell
    Technosailor



  4. #4
    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)
    Sketch, like i always say, doing it like that, what's going to happen when someone types

    [ b]text

    ? the rest of the page will be bold b/c you're not checking for an ending tag. you should use regular expressions (preg_replace()) because:

    1) you can ensure that there's an ending tag.
    2) your tags can be case-insensitive.
    3) it'll be faster (looking through the string once w/ preg_replace() is faster than 2 str_replace()'s )


    but, whatever. just letting ya know...
    - 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

  5. #5
    Prolific Blogger silver trophy Technosailor's Avatar
    Join Date
    Jun 2001
    Location
    Before These Crowded Streets
    Posts
    9,446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dr,

    I see what you're saying. The problem is is that str_replace(), preg_replace and eregi_replace (Which I DO know not to use) are new functions for me. I did not know that about preg_replace and quite honestly, I'm not 100% on the syntax. I've looked at a few preg_replace lines and they scare the crap out of me as I'm not sure what they mean. Perhaps you could give me an example of what you mean as, obviously, instead of doing this right now, I'd like to do this right now.

    Thanks for your help.

    Sketch
    Aaron Brazell
    Technosailor



  6. #6
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Regular expressions really are not my thing, come on Matt show us how! This code from Louie is interesting, allowing you to create an array of patterns and an array of replacements:

    Code:
    $joketext = '[L=http://www.sitepoint.com][B]site point[EB][EL]';
    
    $in = array('/\[L](.+?)\[EL]/is', '/\[L=(.+?)](.+?)\[EL]/is', '/\[B](.+?)\[EB]/is');
    $out = array('<a href="$1">$1</a>', '<a href="$1">$2</a>', '<b>$1</b>');
    
    $joketext = preg_replace($in, $out, $joketext);
    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  7. #7
    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)
    sorry i didn't reply sooner.

    first, in your "code" array, you have /r and /n/r. you could say that those should be \r and \r\n. but, there isn't really any reason to replace \r\n with <p> tags, because \r\n is just a single newline (that happens to be represented by 2 characters on Windows systems). for the newlines to be replaced with <br />, i would use nl2br(). the only problem i've found with that, is that it (nl2br()) doesn't work on just \r newlines (such as the Mac). i remedy that by replacing \r\n or \r with just \n before storing the text in the database (then nl2br() works fine on retrieval).

    first, your other "BBCode" tags via regex (you can stick them in your function or whatever):

    PHP Code:
    $txt nl2br($txt);
    $txt preg_replace('#\\[ i](.+?)\\[/i]#is''<span style="font-style : italic ;">$1</span>'$txt);
    $txt preg_replace('#\\[ b](.+?)\\[/b]#is''<span style="font-style : bold ;">$1</span>'$txt);
    $txt preg_replace('#\\[ u](.+?)\\[/u]#is''<span style="font-style : underline ;">$1</span>'$txt);
    $txt preg_replace('#\\[ list](.+?)\\[/list]#is''<ul>$1</ul>'$txt);
    $txt preg_replace('#\\[\\*](.+?)\\[/\\*]#is''<li>$1</li>'$txt); 
    as you can see, they're all the same except for the text they're matching. it's pretty easy to figure out what's going on after you look at the PHP manual entry for preg.

    i use # as the pattern modifier (this is in the manual) instead of the usual / so that i don't have to escape the literal / in the patterns.

    escape the ['s with a \ so they're matched literally, rather than a character class (again, manual).

    the () capture the text between your tags for use in the replacement (the $1 -- manual).

    the "is" on the end: i makes the pattern case-insensitive. s makes the . (dot) meta-character match newline characters (manual).

    note: replace $1 with \1 if your PHP version is less than 4.0.4.

    now, that regex to use before inserting into MySQL to convert \r\n or \r to just \n so that nl2br() works all the time:

    PHP Code:
    $txt preg_replace('/\r\n|\r/'"\n"$txt); 
    hope that helps some.
    Last edited by DR_LaRRY_PEpPeR; Feb 25, 2002 at 00:40.


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
  •