2017-11-23 12 views
2

私はMessage=Authorにテキストを分割することに基づいてテキストの配列を作成しようとしています。しかし、最初の配列項目として空の値を返します。なぜこのようなことが起こっているのか、頭を丸くすることはできません。だから私はいくつかのアドバイスのためにここに来ることに決めました。最初の項目として配列の空の値(PHP)

$text = "Message=Great school. Great teaching team. School does an amazing job at supporting ALL my child’s needs. They have always gone the extra mile for him. So thank you all. You do a great job! Author=Parent 

Message=Fabulous school with a friendly atmosphere. Staff and the Headteacher are approachable. I cannot praise the school highly enough. Author=Parent 

Message=For the past 9 years, through 2 children, I have always felt NPS goes the extra mile to support it's students in all areas of the curriculum. The staff are fantastic, hard-working and very approachable. It is a great school and I am glad we chose it all those years ago. Author=Parent 

Message=This school has always been a positive influence on my children (2 of which didn't want to leave!) I have recommended this school to several people in the past and will continue to do so in the future. High praise for all the teachers and assistants who make the school what it is! Author=Parent 

Message=Great team of staff at NPS. Ladies at reception are so helpful. Newsletter and Facebook page are very informative and helpful. Breakfast club - excellent - my child raves about it. Love the Friday celebration assembly. The sense of pride and encouragement felt is overwhelming - a wonderful celebration of the children. Author=Parent 

Message=Thanks for the beautiful memories, good times and great learning experiences for Adam, Faith and Rose! Truly has been a life time adventure for the kids...one that will last a lifetime of memories! Hope to visit Neston again in the future! Parent=A parent, after family returned home overseas 

Message=We would like to thank all the teachers who made the Delamere trip possible, Kieran had an amazing time and is already asking when he can go again Author=Parent, following Year 2 residential visit"; 

$tempArray = explode("Message=", $text); 
echo "<pre>"; 
foreach($tempArray as $value){ 
    print_r(explode("Author=", $value)); 
} 
echo "</pre>"; 

結果:

Array 
(
    [0] => 
) 
Array 
(
    [0] => Great school. Great teaching team. School does an amazing job at supporting ALL my child’s needs. They have always gone the extra mile for him. So thank you all. You do a great job! 
    [1] => Parent 


) 
Array 
(
    [0] => Fabulous school with a friendly atmosphere. Staff and the Headteacher are approachable. I cannot praise the school highly enough. 
    [1] => Parent 


) 
Array 
(
    [0] => For the past 9 years, through 2 children, I have always felt NPS goes the extra mile to support it's students in all areas of the curriculum. The staff are fantastic, hard-working and very approachable. It is a great school and I am glad we chose it all those years ago. 
    [1] => Parent 


) 
Array 
(
    [0] => This school has always been a positive influence on my children (2 of which didn't want to leave!) I have recommended this school to several people in the past and will continue to do so in the future. High praise for all the teachers and assistants who make the school what it is! 
    [1] => Parent 


) 
Array 
(
    [0] => Great team of staff at NPS. Ladies at reception are so helpful. Newsletter and Facebook page are very informative and helpful. Breakfast club - excellent - my child raves about it. Love the Friday celebration assembly. The sense of pride and encouragement felt is overwhelming - a wonderful celebration of the children. 
    [1] => Parent 


) 
Array 
(
    [0] => Thanks for the beautiful memories, good times and great learning experiences for Adam, Faith and Rose! Truly has been a life time adventure for the kids...one that will last a lifetime of memories! Hope to visit Neston again in the future! 
    [1] => A parent, after family returned home overseas 


) 
Array 
(
    [0] => We would like to thank all the teachers who made the Delamere trip possible, Kieran had an amazing time and is already asking when he can go again 
    [1] => Parent, following Year 2 residential visit 
) 
+0

array_filter() – kunal

+2

'explode'は文字列を分割します** **指定された区切り文字 - 文字列が区切り文字で始まる(または終わる)場合、結果の一部として空の文字列が得られます。これは、正規表現などではなく、テキスト解析のために使用する(多くの)欠点の1つです。結果を単純に 'if ...'チェックするか、 'array_filter'を使って回避することができます。 – iainn

答えて

3

はこれを説明することができますより短いストリングで。 $text = "Message=Great school. Great teaching team.";

このような文字列を展開すると、$tempArray = explode("Message=", $text);が見つかり、「メッセージ=」が1つ見つかります。 で何かを分割した場合、の1点を後で分割する必要があります。この場合、最初の部分は "Message ="の前に立っていたもので、2番目の部分は "Message ="の後の部分です。しかし、現時点では文字列の先頭にあるので、最初の部分は空の文字列になります。

あなたの$テキストはこのようになる場合:
$text = "FooBar Message=Great school. Great teaching team."; その後、あなたはまだ2枚を持っているだろうが、最初のものは、空ではないだろうが、「FOOBAR」を含んでいるでしょう。

0

array_filter()の使用は、それが(array_filterについての詳細は、あなたの配列

$tempArray = array_filter(explode("Message=", $text)); 
    echo "<pre>"; 
foreach($tempArray as $value){ 
    print_r(explode("Author=", $value)); 
} 
echo "</pre>"; 

をフィルタリングします): - http://php.net/manual/en/function.array-filter.php

関連する問題