0

PHP を使用して、次の文字列「ランダムな単語 [Ab]another few words here [Bx]and yet a [Sq4]few more」を次の配列に分割するにはどうすればよいですか。

 array[0] = 'Random words ' 
 array[1] = '[Ab]'
 array[2] = 'another few words here ' 
 array[3] = '[Bx]' 
 array[4] = 'and yet a ' 
 array[5] = '[Sq4]' 
 array[6] = 'few more'

文字列 [xxx] が区切り文字として機能し、中かっこの間に 1 ~ 4 文字の英数字を含めることができますか?

また、2 つの配列を作成する場合に、同様のことを行うにはどうすればよいでしょうか。

arrayA[0] = 'Random words '
arrayA[1] = 'another few words here '
arrayA[2] = 'and yet a '
arrayA[3] = 'few more'

arrayB[0] = '[Ab]'
arrayB[1] = '[Bx]'
arrayB[2] = '[Sq4]'

ここで何か助けていただければ幸いです。

4

1 に答える 1

1

このパターンはpreg_splitで使用できます

$pattern = '~(?=\[[^]]*])|]\K(?=[^[])~';

またはもっと簡単:

$pattern = '~(?=\[)|]\K(?=[^[])~';

また:

$pattern = '~(?=\[)|(?<=])~';
于 2013-07-04T01:59:23.297 に答える