我有一个包含多行数据的大字符串,如下所示(流名称和流URL)。我想将每一行放入任何数组,然后将每个数组值分隔为流名称和流URL,然后为每个流线构建相应的超链接。任何人都可以告诉我如何做到这一点?
编辑: 我将每一行放入阵列但我不能将每一行分成两部分1!我的名字数组是空的!谁能告诉我什么是错的?
$stream_array = explode( "\n", $file_contents );
$names = array();
foreach( $stream_array as $stream ){
$split = explode( $stream, " " );
array_push( $names, $split[ 0 ] );
};
print_r($stream_array);
print_r($names);
我希望在将$ file_contents的每一行放入数组后,将流名称放入names数组并将url流转换为foo数组。
for($i = 0; $i < count($foo[1]); $i++){
?>
<a href="./doit.php?ID=<?php echo $foo[1][$i] ; ?>&title= <?php echo $names[0][$i] ;?>"><?php echo $i.") "; echo $names[0][$i] ;?> </a> <br />
<?
}//end of for
保存流列表($ file_contents)的大字符串示例:
name1 http://somesite.net/all/name1tv.m3u8
name2 http://somesite.net/all/name2tv.m3u8
name3 http://somesite.net/all/name3tv.m3u8
答案 0 :(得分:1)
它非常简单,并且PHP语法可以为您完成大部分工作: -
// Get a file into an array.
// Each line will be added as a new occurance of the $lines array
$lines = file('filename.ext');
foreach ( $lines as $line ) {
list($name, $url) = explode(' ', $line);
echo $name . PHP_EOL;
echo $url . PHP_EOL;
// of course you will probably want to do something different here
// this just shows you whats going on.
}
结果将是:
name1
http://somesite.net/all/name1tv.m3u8
name2
http://somesite.net/all/name2tv.m3u8
name3
http://somesite.net/all/name3tv.m3u8
答案 1 :(得分:0)
我解决了: - )
$stream_array = explode( "\n", $file_contents );
$names = array();
foreach( $stream_array as $stream ){
//$split = explode( $stream, " " );
$split = preg_split('/\s+/', $stream);
array_push( $names, $split[ 0 ] );
};
print_r($stream_array);
print_r($names);