PHP - 将字符串拆分为数组

时间:2018-03-22 21:43:51

标签: php arrays

我有一个看起来像这样的php字符串......

[line1]this is some test text[/line1][line2]This is line 2 text[/line2][line3]This is line 3 text[/line3]

我试图接受这个并创建一个看起来像这样的数组。

array(
    "line1"=>"this is some test text",
    "line2"=>"This is line 2 text",
    "line3"=>"This is line 3 text"
)

字符串是动态创建的,因此它可以包含第1行 - 第99行,依此类推。

这样做的最佳方法是什么,并保持可扩展性?有没有人可以指出我的例子?

2 个答案:

答案 0 :(得分:2)

就正则表达式而言,这可能是匹配模式的合理折衷。

注意:这不会处理嵌套/递归。

\[([^\]]+)\](.*?)\[/\g{1}\]

用法:

preg_match_all( '%\[([^\]]+)\](.*?)\[/\g{1}\]%', $subject, $matches, PREG_SET_ORDER );
var_dump( $matches );
Match the character “[” literally «\[»
Match the regex below and capture its match into backreference number 1 «([^\]]+)»
   Match any character that is NOT a “]” «[^\]]+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
Match the character “]” literally «\]»
Match the regex below and capture its match into backreference number 2 «(.*)»
   Match any single character that is NOT a line break character (line feed) «.*»
      Between zero and unlimited times, as many times as possible, giving back as needed (lazy) «*»
Match the character “[” literally «\[»
Match the character “/” literally «/»
Match the same text that was most recently matched by capturing group number 1 (case sensitive; fail if the group did not participate in the match so far) «\g{1}»
Match the character “]” literally «\]»

答案 1 :(得分:0)

您可以将此方法(通过多次分割输入字符串)与explode一起使用,以隔离相关数据: line-id line-text < /强>:

$text = "[line1]this is some test text[/line1][line2]This is line 2 text[/line2][line3]This is line 3 text[/line3]";

$text = explode( '][line', ']'.$text.'[line');

// you'll get a spurious item at index 0 and at the end, they'll be skipped

$n = count( $text );

$output = array();

for( $i = 1; $i < $n - 1; $i++ )
{
    $line = explode( ']', $text[ $i] );
    $id = 'line' . $line[ 0 ];
    $line = explode( '[', $line[ 1 ] );
    $value = $line[ 0 ];
    $output[ $id ] = $value;
}

var_export( $output );
echo "\n";

你得到:

array (
  'line1' => 'this is some test text',
  'line2' => 'This is line 2 text',
  'line3' => 'This is line 3 text',
)

注意:

可以容忍并正确处理空“行”

line文本中的方形制动会破坏代码并将所有内容搞砸。

输入格式必须采用

格式

[line 名词 ] 文本 [/line <强> 名词 ] ...

如果您有其他要求,可以调整代码。

我认为这可能是一个很好的起点。

请注意(再次):

这是一个有效的解决方案(考虑到上面提到的重要意义)。

另一种方法是使用正则表达式和preg_match_all()使用捕获组来检索行ID和行文本。