Domain Empire

Using PHP explode() with seperator being \n?

NameSilo
Watch
Hi,

Can I use explode() so that the seperator is a line return ... a new line... etc... something like this:

explode($string, '\n');

Would that work? What I want to do is split a textbox with this data:
______________________________

1041 Parkway Lane
Chicago, Illinois 55102

______________________________

So that "1041 Parkway Lane" and "Chicago, Illinois 55102" are split into the array, meaning that I need explode() to seperate it at \n, right?

Thanks, all help appreciated.
-Matt
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
EDIT: yes it will work->

it will be explode("\n",$string) NOT explode($string, "\n")
 
0
•••
Er, yeah, gotcha ;) Thanks.
 
0
•••
Well, just for archive and future Search purposes (some may find this thread useful), here's how I did it:

\n did not work, instead I actually did something like this:
PHP:
$array = explode('
', $string)

So I actually pressed "Enter" in the code to create a line break. That worked ^_^
 
0
•••
compuXP said:
Well, just for archive and future Search purposes (some may find this thread useful), here's how I did it:

\n did not work, instead I actually did something like this:
PHP:
$array = explode('
', $string)

So I actually pressed "Enter" in the code to create a line break. That worked ^_^

i think you have to use double quote with \n becase you are using substitution
 
0
•••
Amnezia said:
i think you have to use double quote with \n becase you are using substitution
Actually, '\n' is a character literal. It does not require substitution.
 
0
•••
0
•••
Try this:
PHP:
$newline = '
';
print( "Line 1" . $newline . "Line 2" );
It's not perfect, but it will work while keeping your code looking clean.
 
0
•••
Hey that's a good idea. Thanks! ^_^
 
0
•••
0
•••
Both work, explode("\n", $var) and explode("
", $var);... the first is just cleaner.
 
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back