1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Converting "Enters" to "Commas"

Discussion in 'PHP' started by Lucky Bastard, Mar 23, 2005.

  1. #1
    Question, what would be the best way to do the following :
    I have a html text area field, where people are to enter items separated by "Enters". When they then post it back to the server, how can I convert the "Enter" separated list to be "Comma" separated, IF in fact they used "Enters".

    The logic I want to perform is as below :
    If (strpos(List,"Enter")) { replace enters with commas } else { }

    Note, I also can't figure out how to do a strpos to see if the returned List has "Enters" as delimiters, and would love to know how to do it "\n" doesn't work for me.
     
    Lucky Bastard, Mar 23, 2005 IP
  2. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #2
    Did you check for "\r" too?
     
    noppid, Mar 23, 2005 IP
  3. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,333
    Likes Received:
    2,613
    Best Answers:
    462
    Trophy Points:
    710
    Digital Goods:
    29
    #3
    Also, make sure you enclose the \r like so: "\r" and *not* like so: '\r'.
     
    digitalpoint, Mar 23, 2005 IP
  4. Lucky Bastard

    Lucky Bastard Peon

    Messages:
    406
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks for the replies, I only did a strpos for "\n".eg:
    strpos($entered_field,"\n") but that didn't work.

    Also how can I replace?
    Just str_replace("\n",",",$entered_field)

    When I tried the above it seemed to leave some spaces/linefeeds still.
     
    Lucky Bastard, Mar 23, 2005 IP
  5. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #5
    noppid, Mar 23, 2005 IP
  6. Lucky Bastard

    Lucky Bastard Peon

    Messages:
    406
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thanks.

    Is my strpos done correctly?

    strpos($entered_field,"\n")

    For whatever reason it returns a "false" not found on a string that has "enters" entered in a text area field.
     
    Lucky Bastard, Mar 23, 2005 IP
  7. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #7
    I would use this strstr($entered_field, "\n") .

    I don't care for the handling of FALSE returns by strpos().

    http://us3.php.net/manual/en/function.strstr.php

    Of course we can't see the previous code to this operation, so alot is assumed.
     
    noppid, Mar 23, 2005 IP
  8. nullbit

    nullbit Peon

    Messages:
    489
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Mac, Windows, and *nix all have different newline characters. For it to work correctly you need to allow for all.
    
    Mac: \r
    Win: \r\n
    *nix: \n
    
    Code (markup):
    This will work:

    
    $text = preg_replace('[\r\n]+', ',', $text);
    
    PHP:
    Just to note: Although DP said you should use double quotes, this does not apply here, he's talking about literal string replacement ( e.g. str_replace() ), not regex replacement. The above using single quotes works since the \r and \n are passed through the parameter as literal characters, and the regex engine will interpret the escape sequence.
     
    nullbit, Mar 23, 2005 IP
  9. Lucky Bastard

    Lucky Bastard Peon

    Messages:
    406
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #9
    I need to do a strpos though cos I WANT TO detect the presence of "Enters" before I do a str_replace.

    BTW, thanks for the help. So $text = preg_replace ('[\r\n]+', ',', $text); is better than doing a str_replace?
     
    Lucky Bastard, Mar 23, 2005 IP
  10. nullbit

    nullbit Peon

    Messages:
    489
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Technically it's cleaner code. But I should say, in theory it might be slower, although the difference will be marginal.
     
    nullbit, Mar 23, 2005 IP
  11. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #11
    Yes, it's said to be faster, but on larger strings.


    
    if( strstr($entered_field, "\n") )
    {
    	$entered_field  = str_replace( "\n", ',', $entered_field );
    	trim($entered_field); 
    }
    elseif( strstr($entered_field, "\r") )
    {
    	$entered_field  = str_replace( "\r", ',', $entered_field );
    	trim($entered_field); 
    }
    
    
    
    PHP:
     
    noppid, Mar 23, 2005 IP
  12. nullbit

    nullbit Peon

    Messages:
    489
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #12
    noppid's code above looks good. But I'd suggest revising the elseif to an if, since currently that will not strip the entire Windows' newline sequence (i.e. it would leave all the \r characters behind)
     
    nullbit, Mar 23, 2005 IP
  13. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #13
    That would contradict the trim function documentation if I understand correctly.

    http://de3.php.net/manual/en/function.trim.php
     
    noppid, Mar 23, 2005 IP
  14. nullbit

    nullbit Peon

    Messages:
    489
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #14
    The problem is trim will not strip any newline characters in the middle of the string only at the start and end of the string.
     
    nullbit, Mar 23, 2005 IP
  15. Lucky Bastard

    Lucky Bastard Peon

    Messages:
    406
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Also wouldn't doing it as a separate "If" and not "else if" on Windows server convert:
    1
    2
    3
    4
    .. to 1,,2,,3,,4 Because as was mentioned on first page Windows is "\n\r"?? Or am I missing something.
     
    Lucky Bastard, Mar 23, 2005 IP
  16. nullbit

    nullbit Peon

    Messages:
    489
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #16
    The preg_match usage I posted earlier is really the simplest option. Windows is "\r\n".
     
    nullbit, Mar 23, 2005 IP
  17. noppid

    noppid gunnin' for the quota

    Messages:
    4,246
    Likes Received:
    232
    Best Answers:
    0
    Trophy Points:
    135
    #17
    yep, I sure missed that. Been using it wrong a while. :D
     
    noppid, Mar 23, 2005 IP
  18. Lucky Bastard

    Lucky Bastard Peon

    Messages:
    406
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #18
    So something like this:
    if (strstr($entered_field, "\n") || strstr($entered_field, "\n")) {
    $entered_field = preg_replace('[\r\n]+', ',', $entered_field);
    }

    Is that right?
     
    Lucky Bastard, Mar 23, 2005 IP
  19. nullbit

    nullbit Peon

    Messages:
    489
    Likes Received:
    19
    Best Answers:
    0
    Trophy Points:
    0
    #19
    Almost, this would work:
    
    if(strpos($entered_field, array("\n", "\r")) !== false) 
    {
      $entered_field = preg_replace('[\r\n]+', ',', $entered_field);
    }
    
    PHP:
    strpos accepts an array for the needle argument. It will check for both "\n" and "\r" in the above code and return the first offset if atleast one is found. The !== operator is used, since if a newline is found at the start of the string strpos returns 0 which evaluates to false in PHP.
     
    nullbit, Mar 23, 2005 IP
  20. Lucky Bastard

    Lucky Bastard Peon

    Messages:
    406
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #20
    Thanks will give it a try, why out of curiosity was this done earlier:
    if( strstr($entered_field, "\n") ) and not if (strpos()) - does the strstr in this example act like a strpos and do a check to see if "\n" exists, and return either true or false? My understanding was that it was more liek a str_replace than a strpos. strtr ( string str, string from, string to ) from :
    http://au2.php.net/manual/en/function.strtr.php
     
    Lucky Bastard, Mar 23, 2005 IP