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条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度