Jump to content

separate query using newlines (retrieving not inserting)


Rokit

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.