PHP substr 函数在 foreach 中没有正确解析
PHP substr function does not parse properly in foreach
我有一些包含类似格式的文件如下:
Nearest Location:
771 km S 43° E of Quezon City
051 km S 66° E of Surigao City
007 km S 51° W of Socorro (Surigao Del Norte)
049 km N 70° E of PFZ EAST MINDANAO SEGMENT
我所做的是通过file_get_contents()
和explode("\n",...)
加载每个文件。所以我用了foreach($rows_location as $row_location)
存到$content_location = $row_location;
。我把它分成两部分,距离和方位信息,以及忽略第一行的位置,如下所示:
$distance_bearing = substr($content_location,0,18);
$location = substr($content_location, 18);
但问题是它只适用于第一行,所以我将它们分开保存并得到
771 km S 43° E of
对于$distance_bearing
和
Quezon City
051 km S 66° E of Surigao City
007 km S 51° W of Socorro (Surigao Del Norte)
049 km N 70° E of PFZ EAST MINDANAO SEGMENT
对于 $location
。
我尝试将每一行转换为 utf8_decode
,因为它包含一个度数符号,它只是返回相同但度数替换为 ?
。我还检查了 $content_location
是否正确存储行并得到:
Nearest Location:
771 km S 43° E of Quezon City
051 km S 66° E of Surigao City
007 km S 51° W of Socorro (Surigao Del Norte)
049 km N 70° E of PFZ EAST MINDANAO SEGMENT
所以我猜它存储正确。
我不知道问题出在哪里,所以任何想法都会有所帮助。预先感谢您的帮助。
抱歉各位,我忘了输入所需的输出,它应该是:
771 km S 43° E of
051 km S 66° E of
007 km S 51° W of
049 km N 70° E of
对于$distance_bearing
和
Quezon City
Surigao City
Socorro (Surigao Del Norte)
PFZ EAST MINDANAO SEGMENT
对于 $location
。
再次感谢!
preg_match_all()
将简单地处理文本块。
代码:(Demo)
$string='Nearest Location:
771 km S 43° E of Quezon City
051 km S 66° E of Surigao City
007 km S 51° W of Socorro (Surigao Del Norte)
049 km N 70° E of PFZ EAST MINDANAO SEGMENT';
if(preg_match_all('/^(.*? of) \K.+/m',$string,$out)){
list($location,$distance_bearing)=$out;
}
var_export($distance_bearing);
echo "\n\n";
var_export($location);
输出:
array (
0 => '771 km S 43° E of',
1 => '051 km S 66° E of',
2 => '007 km S 51° W of',
3 => '049 km N 70° E of',
)
array (
0 => 'Quezon City',
1 => 'Surigao City',
2 => 'Socorro (Surigao Del Norte)',
3 => 'PFZ EAST MINDANAO SEGMENT',
)
图案说明:
/ # start the pattern
^ # match from the start of the line
(.*? of) # match the leading text upto and including the first occurrence of `of`
\K.+ # match 1 space, then restart the fullstring match using \K then match the rest of the string
/ # end the pattern
m # force ^ character to match every line start instead of string start
我有一些包含类似格式的文件如下:
Nearest Location: 771 km S 43° E of Quezon City 051 km S 66° E of Surigao City 007 km S 51° W of Socorro (Surigao Del Norte) 049 km N 70° E of PFZ EAST MINDANAO SEGMENT
我所做的是通过file_get_contents()
和explode("\n",...)
加载每个文件。所以我用了foreach($rows_location as $row_location)
存到$content_location = $row_location;
。我把它分成两部分,距离和方位信息,以及忽略第一行的位置,如下所示:
$distance_bearing = substr($content_location,0,18);
$location = substr($content_location, 18);
但问题是它只适用于第一行,所以我将它们分开保存并得到
771 km S 43° E of
对于$distance_bearing
和
Quezon City
051 km S 66° E of Surigao City
007 km S 51° W of Socorro (Surigao Del Norte)
049 km N 70° E of PFZ EAST MINDANAO SEGMENT
对于 $location
。
我尝试将每一行转换为 utf8_decode
,因为它包含一个度数符号,它只是返回相同但度数替换为 ?
。我还检查了 $content_location
是否正确存储行并得到:
Nearest Location:
771 km S 43° E of Quezon City
051 km S 66° E of Surigao City
007 km S 51° W of Socorro (Surigao Del Norte)
049 km N 70° E of PFZ EAST MINDANAO SEGMENT
所以我猜它存储正确。
我不知道问题出在哪里,所以任何想法都会有所帮助。预先感谢您的帮助。
抱歉各位,我忘了输入所需的输出,它应该是:
771 km S 43° E of
051 km S 66° E of
007 km S 51° W of
049 km N 70° E of
对于$distance_bearing
和
Quezon City
Surigao City
Socorro (Surigao Del Norte)
PFZ EAST MINDANAO SEGMENT
对于 $location
。
再次感谢!
preg_match_all()
将简单地处理文本块。
代码:(Demo)
$string='Nearest Location:
771 km S 43° E of Quezon City
051 km S 66° E of Surigao City
007 km S 51° W of Socorro (Surigao Del Norte)
049 km N 70° E of PFZ EAST MINDANAO SEGMENT';
if(preg_match_all('/^(.*? of) \K.+/m',$string,$out)){
list($location,$distance_bearing)=$out;
}
var_export($distance_bearing);
echo "\n\n";
var_export($location);
输出:
array (
0 => '771 km S 43° E of',
1 => '051 km S 66° E of',
2 => '007 km S 51° W of',
3 => '049 km N 70° E of',
)
array (
0 => 'Quezon City',
1 => 'Surigao City',
2 => 'Socorro (Surigao Del Norte)',
3 => 'PFZ EAST MINDANAO SEGMENT',
)
图案说明:
/ # start the pattern
^ # match from the start of the line
(.*? of) # match the leading text upto and including the first occurrence of `of`
\K.+ # match 1 space, then restart the fullstring match using \K then match the rest of the string
/ # end the pattern
m # force ^ character to match every line start instead of string start