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.
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.
$result = str_replace( "\n", ',', $entered_field ) If it's not clean then, run it through trim(). http://us3.php.net/manual/en/function.trim.php
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.
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.
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.
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?
Technically it's cleaner code. But I should say, in theory it might be slower, although the difference will be marginal.
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'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)
That would contradict the trim function documentation if I understand correctly. http://de3.php.net/manual/en/function.trim.php
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.
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.
So something like this: if (strstr($entered_field, "\n") || strstr($entered_field, "\n")) { $entered_field = preg_replace('[\r\n]+', ',', $entered_field); } Is that right?
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.
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