dquoj04882 2017-05-10 23:07
浏览 97
已采纳

如何使用新行或行尾将mysql数据库中的字符串拆分为2个部分?

I have a variable that gets set from a database field that may have multiple line breaks. I want some php code that splits the text from that variable into 2 separate variables (or a array with 2 values) using ONLY the first line break as the delimiter.

So you understand what I am trying to do, the database field the variable is coming from is a post message and I essentially want to use the first bit of text up to the first line break as the post title and everything after that as the post message.

An example would be as follows:

$myvariable = "Do you believe what happened today? This great big bus almost ran me over. Then I jumped out of the way. Then something else happened and it was more crazy. Finally I decide to go home and have a nap because the day was just going too crazy. I like elephant pants, the fit me SNUGG!";

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. Then something else happened and it was more crazy. Finally I decide to go home and have a nap because the day was just going too crazy. I like elephant pants, the fit me SNUGG!";

I have looked at a few ways to do this using as the delimiter and using explode, and I have looked at PHP_EOL using explode as well.

There are a few great threads about this like the following:

How to put string in array, split by new line?

Explode PHP string by new line

The problem is if I use code like:

$new_array_with_new_variables = explode("
", $myvariable);

or

$new_array_with_new_variables = explode("PHP_EOL", $myvariable);

I end up splitting EVERY new line into a variable...

Any suggestions super coders out there? I just can't figure this one out!

  • 写回答

2条回答 默认 最新

  • douliwang6896 2017-05-10 23:11
    关注

    The following will do what you are looking for.

    $new_array_with_new_variablesTMP = explode("
    ", $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('
    ', $new_array_with_new_variablesTMP));
    
    print_r($new_array_with_new_variables);
    

    Result:

    Array
    (
        [0] => Do you believe what happened today?
        [1] => This great big bus almost ran me over. Then I jumped out of the way.
    Then something else happened and it was more crazy.
    Finally I decide to go home and have a nap because the day was just going too crazy.
    I like elephant pants, the fit me
    SNUGG!
    )
    

    See it live: http://sandbox.onlinephpfunctions.com/code/0ff2eeb5fb73593a2baa6b6abd88878281ac9acf

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog