【问题标题】:How do i start str_split just in the middle of characters text line我如何在字符文本行的中间开始 str_split
【发布时间】:2017-11-01 21:05:03
【问题描述】:

我有一个 100 万个字符的文本行,我有 PART2 就在文本行的中间,也就是 500 个字符 ' PART2 ' 之前的 s 个字符和 ' PART2 ' 之后的 500 个字符 我的问题是我如何从' PART2 ' 的位置开始 str_split 。在文本行中 例子

 `5_hundred_thusands_text_charactersPART2fivehundredthusandstextcharactersagain`

str_split 之后的结果输出应该从“PART2”开始 'fivehundre','dthusandte','xtcharacte' 这些是我的代码:

 $txt = "5_hundred_thusands_text_charactersPART2fivehundredthusandstextcharactersagain";
   if(preg_match('/(START)/i', $txt))      {
  #start from there
  $see = str_split($txt, 10);
   echo " ','$see"; 
   }

我使用这段代码的结果只是从一开始我真的可以在 php 中实现吗?感谢您阅读并影响我的解决方案

【问题讨论】:

标签: php string text split


【解决方案1】:

您可以通过preg_split 使用以下解决方案:

$txt = "5_hundred_thusands_text_charactersPART2fivehundredthusandstextcharactersagain";
$arrParts = preg_split("/PART2/", $txt);

$arrSplitItems = str_split($arrParts[1], 10);
echo implode(',', $arrSplitItems);

演示: https://ideone.com/qx0KdS


注意:除了使用preg_split,您还可以使用explode

$arrParts = explode('PART2', $txt);

【讨论】:

  • 我真的不需要数组形式的结果
  • 所以使用implodestr_split 返回一个数组。查看更新
【解决方案2】:

我会使用explode,它实际上比preg_split 快,而且你不需要正则表达式,因为你知道分隔符是什么。

$txt = "5_hundred_thusands_text_charactersPART2fivehundredthusandstextcharactersagain";
$r = explode("PART2", $txt);
$see = str_split($r[1], 10);
print_r($see); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 2015-11-25
    • 2013-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多