dquoj04882 2017-05-10 15: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 15: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 fluentmeshing
  • ¥15 手机/平板的浏览器里如何实现类似荧光笔的效果
  • ¥15 盘古气象大模型调用(python)
  • ¥15 传人记程序做的plc 485从机程序该如何写
  • ¥15 已知手指抓握过程中掌指关节、手指各关节和指尖每一帧的坐标,用贝塞尔曲线可以拟合手指抓握的运动轨迹吗?
  • ¥50 libwebsockets 如何添加其他socket事件回调
  • ¥50 实现画布拖拽算子排布,通过flink实现算子编排计算,请提供思路
  • ¥15 esium自定义材质拉伸问题
  • ¥15 cmake+mingw使用<mysqlx/xdevapi.h>报错
  • ¥15 eNSP中防火墙的使用
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部