如何使用新行或行尾将 mysql 数据库中的字符串分成两部分?
How to split string from mysql database into 2 parts using new line or end of line?
我有一个从可能有多个换行符的数据库字段中设置的变量。我想要一些 php 代码,仅使用第一个换行符作为分隔符,将文本从该变量拆分为 2 个单独的变量(或具有 2 个值的数组)。
所以你明白我想做什么,变量来自的数据库字段是一条 post 消息,我基本上想使用文本的第一位到第一行中断作为post 标题和之后的所有内容作为 post 消息。
示例如下:
$myvariable = "Do you believe what happened today?\nThis great big bus
almost ran me over. Then I jumped out of the way.\nThen something else
happened and it was more crazy.\nFinally I decide to go home and have
a nap because the day was just going too crazy.\nI like elephant
pants, the fit me\nSNUGG!";
My new variables would become:
$title_from_myvariable = "Do you believe what happened today?";
$message_from_myvariable = "This great big bus almost ran me over.
Then I jumped out of the way.\nThen something else happened and it was
more crazy.\nFinally I decide to go home and have a nap because the
day was just going too crazy.\nI like elephant pants, the fit
me\nSNUGG!";
我研究了几种使用 \n 作为分隔符并使用 explode 来执行此操作的方法,我还研究了 PHP_EOL 使用 explode。
有一些关于此的很棒的话题,如下所示:
How to put string in array, split by new line?
Explode PHP string by new line
问题是如果我使用如下代码:
$new_array_with_new_variables = explode("\n", $myvariable);
或
$new_array_with_new_variables = explode("PHP_EOL", $myvariable);
我最终将 EVERY 新行拆分为一个变量...
超级程序员有什么建议吗?我就是想不通这个!
以下将满足您的需求。
$new_array_with_new_variablesTMP = explode("\n", $myvariable);
$new_array_with_new_variables = array($new_array_with_new_variablesTMP[0]);
array_shift($new_array_with_new_variablesTMP);
array_push($new_array_with_new_variables, implode('\n', $new_array_with_new_variablesTMP));
print_r($new_array_with_new_variables);
结果:
Array
(
[0] => Do you believe what happened today?
[1] => This great big bus almost ran me over. Then I jumped out of the way.\nThen something else happened and it was more crazy.\nFinally I decide to go home and have a nap because the day was just going too crazy.\nI like elephant pants, the fit me\nSNUGG!
)
现场观看: http://sandbox.onlinephpfunctions.com/code/0ff2eeb5fb73593a2baa6b6abd88878281ac9acf
这是一个有效的方法。
<?php
$pieces = explode("\n", $myvariable);
$title_from_myvariable = $pieces[0];
$message_from_myvariable = implode("\n", array_slice($pieces, 1, count($pieces) - 1));
我有一个从可能有多个换行符的数据库字段中设置的变量。我想要一些 php 代码,仅使用第一个换行符作为分隔符,将文本从该变量拆分为 2 个单独的变量(或具有 2 个值的数组)。
所以你明白我想做什么,变量来自的数据库字段是一条 post 消息,我基本上想使用文本的第一位到第一行中断作为post 标题和之后的所有内容作为 post 消息。
示例如下:
$myvariable = "Do you believe what happened today?\nThis great big bus almost ran me over. Then I jumped out of the way.\nThen something else happened and it was more crazy.\nFinally I decide to go home and have a nap because the day was just going too crazy.\nI like elephant pants, the fit me\nSNUGG!";
My new variables would become:
$title_from_myvariable = "Do you believe what happened today?";
$message_from_myvariable = "This great big bus almost ran me over. Then I jumped out of the way.\nThen something else happened and it was more crazy.\nFinally I decide to go home and have a nap because the day was just going too crazy.\nI like elephant pants, the fit me\nSNUGG!";
我研究了几种使用 \n 作为分隔符并使用 explode 来执行此操作的方法,我还研究了 PHP_EOL 使用 explode。
有一些关于此的很棒的话题,如下所示:
How to put string in array, split by new line?
Explode PHP string by new line
问题是如果我使用如下代码:
$new_array_with_new_variables = explode("\n", $myvariable);
或
$new_array_with_new_variables = explode("PHP_EOL", $myvariable);
我最终将 EVERY 新行拆分为一个变量...
超级程序员有什么建议吗?我就是想不通这个!
以下将满足您的需求。
$new_array_with_new_variablesTMP = explode("\n", $myvariable);
$new_array_with_new_variables = array($new_array_with_new_variablesTMP[0]);
array_shift($new_array_with_new_variablesTMP);
array_push($new_array_with_new_variables, implode('\n', $new_array_with_new_variablesTMP));
print_r($new_array_with_new_variables);
结果:
Array
(
[0] => Do you believe what happened today?
[1] => This great big bus almost ran me over. Then I jumped out of the way.\nThen something else happened and it was more crazy.\nFinally I decide to go home and have a nap because the day was just going too crazy.\nI like elephant pants, the fit me\nSNUGG!
)
现场观看: http://sandbox.onlinephpfunctions.com/code/0ff2eeb5fb73593a2baa6b6abd88878281ac9acf
这是一个有效的方法。
<?php
$pieces = explode("\n", $myvariable);
$title_from_myvariable = $pieces[0];
$message_from_myvariable = implode("\n", array_slice($pieces, 1, count($pieces) - 1));