Rokit Posted September 29, 2011 Share Posted September 29, 2011 First, here's some code: $result = mysql_query("SELECT standard FROM thestandards WHERE id={$theSearch}", $connection); if (!$result) { die("Database connection failed: " . mysql_error()); } while ($row = mysql_fetch_array($result)) { $query = $row[0]; } // echo $testing = nl2br($query); $subStrings = explode('\n', $query); echo $subStrings[0] . "<br />"; // outputs entire query echo $subStrings[1] . "<br />"; // undefined offset echo $subStrings[2] . "<br />"; // undefined offset echo $subStrings[3] . "<br />"; // undefined offset So, the data I'm retrieving from the database is several small paragraphs. I want to take these paragraphs, separate them, and put them into an array. I tried using the explode function with the newline char, but for some reason it doesn't work. I can get it to work if I want to go explicitly add "\n"s in the database everywhere, but that just doesn't seem practical. nl2br doesn't work for what I need, but I find it interesting that this function is somehow able to "see" all the newlines in the query, whereas the explode function cannot. Explode is really what I need, but I've tried '\n', '\r', '\r\n' and nothing works. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/248140-separate-query-using-newlines-retrieving-not-inserting/ Share on other sites More sharing options...
codeprada Posted September 29, 2011 Share Posted September 29, 2011 '\n' is not the newline character but a string of '\' and 'n'. Note the single quotes as you have. Use double quotes so that PHP will parse it as a new line character. Quote Link to comment https://forums.phpfreaks.com/topic/248140-separate-query-using-newlines-retrieving-not-inserting/#findComment-1274207 Share on other sites More sharing options...
Psycho Posted September 29, 2011 Share Posted September 29, 2011 Although codeprada's statements are correct - you are doing it wrong! There is a built-in PHP function called nl2br() which will convert any line breaks in a string into BR tags. besides, what you had above was unnecessary. You could have done an explode() immediately followed by an implode using '<br>' as the glue. Quote Link to comment https://forums.phpfreaks.com/topic/248140-separate-query-using-newlines-retrieving-not-inserting/#findComment-1274210 Share on other sites More sharing options...
Rokit Posted September 29, 2011 Author Share Posted September 29, 2011 By God it worked. Seems no matter what programming language I've always been able to use ' and " interchangeably. Thanks for the help. Quote Link to comment https://forums.phpfreaks.com/topic/248140-separate-query-using-newlines-retrieving-not-inserting/#findComment-1274213 Share on other sites More sharing options...
Caliber Mengsk Posted September 29, 2011 Share Posted September 29, 2011 yeah, I was going to agree with codeproda A letter surrounded by ' generally means character, where as surrounded by " means string. The odd part is though that most commands allow for 'word' to be a string, for example, echo can. I feel that the reason for that is it's seeing the 'word' as an array. 'w' 'o' 'r' 'd' And, I feel that most commands simply parse a character array as a string, but some commands (like explode) need it to be implicit... O-o. To be honest though, I would have figured your code to work even with the ' instead of ". I guess it depends on the os and version of PHP. Quote Link to comment https://forums.phpfreaks.com/topic/248140-separate-query-using-newlines-retrieving-not-inserting/#findComment-1274214 Share on other sites More sharing options...
Psycho Posted September 29, 2011 Share Posted September 29, 2011 I guess it depends on the os and version of PHP. Not at all. There are specific differences between single quoted stings and double quoted strings that are clearly described in the manual. The biggest difference is that certain "elements" will be interpreted inside double quoted string. For example $foo = 'bar'; echo 'Foo:\t$foo'; Will output the literal text "Foo:\t$foo" because neither the escaped tab character nor the variable $foo will be interpreted inside the string. However, if you used a double quoted string: $foo = 'bar'; echo "Foo:\t$foo"; The output would be: Foo: bar because both the escaped tab character and the variable $foo will be interpreted. More info here: http://php.net/manual/en/language.types.string.php Quote Link to comment https://forums.phpfreaks.com/topic/248140-separate-query-using-newlines-retrieving-not-inserting/#findComment-1274220 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.