douyi5157 2017-10-26 11:27
浏览 135
已采纳

用某种空格分割字符串

I've got the following code:

$output2 = 'text

more text

ja';
$explode = explode('

', $output);

This works fine and the $explode array prints the following:

Array
(
    [0] => text
    [1] => meer text
    [2] => ja
)

However, the following code doesn't work and I don't know how to fix it:

$output = 'text



more text



ja';

$explode = explode('

', $output);

The $explode array prints the following:

Array
(
    [0] => text
    

    
  //more text
    

    
ja
)

This might seem like a weird question. But the first example is a test I made manually. But the second example is what is actually returned from the database.

  • 写回答

5条回答 默认 最新

  • dongzhuandian3292 2017-10-26 11:36
    关注

    You can use preg_split to split your string:

    <?php
    $output = 'text
    &NewLine;
    &NewLine;more text
    &NewLine;
    &NewLine;ja';
    
    $explode = preg_split('/(&NewLine;|(
    ||
    ))+/', $output, -1, PREG_SPLIT_NO_EMPTY);
    

    demo: https://ideone.com/KU0v9t (ideone) or https://eval.in/887393 (eval.in)


    The following solution to split on double &NewLine;:

    $output = 'text
    &NewLine;
    &NewLine;more text
    &NewLine;
    &NewLine;ja
    &NewLine;nein';
    
    $explode = preg_split('/(
    ||
    )*(&NewLine;(
    ||
    )*){2}/', $output, -1, PREG_SPLIT_NO_EMPTY);
    

    demo: https://ideone.com/0txh5O

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

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号