explode on \n

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

explode on \n

Post by shiznatix »

Am I just not seeing it? Why can I not explode on a \n? I know 100% that its there cause it comes out of the db and i know it has it in the db and when i <pre>print_r($arr)</pre> it has the line break and when i view source it does as well but when I explode on it, nothing happens.

Code: Select all

$fields = explode(' \n ', $info[1]);

echo 'fields';
$this->dump($fields);
/*
* [0] => Field 3 = params 3  
*            Field 4 = params 4  
*/
thats what happens. Any help? I really would like to keep the \n but even more so need somthing to explode on to seperate the 2 parts there.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Have you tried cutting out the spaces around the \n and also trying it in double quotes instead of single?

I have been able to explode on \n before without any hassle so it must be simple :)
sheila
Forum Commoner
Posts: 98
Joined: Mon Sep 05, 2005 9:52 pm
Location: Texas

Post by sheila »

You need double quotes around the \n

Code: Select all

$fields = explode("\n", $info[1]);
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

If you want to keep the \n how about this? It's OS independent too.

Code: Select all

<?php
$parts = preg_split('/$/m', $string);
?>
EDIT | Ah yes, I should have read the responses better.... string literal won't parse \n ;)
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

stupid double quotes. makes sence though. too much coding for one day thanks for the responces
Post Reply