jj20051 Posted December 4, 2011 Share Posted December 4, 2011 I have a script that connects to another server to pull data... When it pulls the data it comes back in the following format: t1.sh t2.sh t3.sh test.sh However using the code bellow does not separate it like it should, it should put one file per line but I think I did something wrong... // Pulls the output from the server: $var_test = $ssh->exec('./test.sh'); // Explodes it... $var_test2 = explode(" ", $var_test); // Echos it one per line... foreach ($var_test2 as $variable) { echo '<br>'.$variable; } Quote Link to comment https://forums.phpfreaks.com/topic/252476-problem-with-explode/ Share on other sites More sharing options...
KevinM1 Posted December 4, 2011 Share Posted December 4, 2011 What is it actually outputting? Quote Link to comment https://forums.phpfreaks.com/topic/252476-problem-with-explode/#findComment-1294447 Share on other sites More sharing options...
jj20051 Posted December 4, 2011 Author Share Posted December 4, 2011 Its outputting the following in html: <br>t1.sh t2.sh t3.sh test.sh Which means it thinks there is only one row in the array or something? Quote Link to comment https://forums.phpfreaks.com/topic/252476-problem-with-explode/#findComment-1294449 Share on other sites More sharing options...
KevinM1 Posted December 4, 2011 Share Posted December 4, 2011 What are you actually trying to accomplish? Because, aside from the visible <br> that's before t1.sh, it looks like it's working properly. Quote Link to comment https://forums.phpfreaks.com/topic/252476-problem-with-explode/#findComment-1294453 Share on other sites More sharing options...
Adam Posted December 5, 2011 Share Posted December 5, 2011 There should be a "<br>" before each file name. I suspect the data is split by new line feeds, not spaces. That could explain the results you're seeing.. try splitting by this instead: preg_split('/(\n|\n\r)/', $var_test); That will use a regular expression to split the string on either "\n" (Linux format line-feed) or "\n\r" (Windows format). Quote Link to comment https://forums.phpfreaks.com/topic/252476-problem-with-explode/#findComment-1294458 Share on other sites More sharing options...
jj20051 Posted December 5, 2011 Author Share Posted December 5, 2011 Thank you Adam... that did exactly what I was looking for it to do It appears that linux sends back the \n even though it looks like a space... Quote Link to comment https://forums.phpfreaks.com/topic/252476-problem-with-explode/#findComment-1294459 Share on other sites More sharing options...
Adam Posted December 5, 2011 Share Posted December 5, 2011 Browsers will convert any whitespace (tabs, new-lines, etc.) into a single space when rendered. If you look at the data into the source it will be new lines. Quote Link to comment https://forums.phpfreaks.com/topic/252476-problem-with-explode/#findComment-1294462 Share on other sites More sharing options...
Adam Posted December 5, 2011 Share Posted December 5, 2011 Whoops made a little mistake with the line-feed characters; should be "\r\n", not "\n\r". Quote Link to comment https://forums.phpfreaks.com/topic/252476-problem-with-explode/#findComment-1294463 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.