几个小时以来,我一直在尝试将 foreach 与 2 个数组一起使用。这是我的代码:
function displayTXTList($fileName) {
if (file_exists($fileName)) {
$contents = file($fileName);
$string = implode($contents);
preg_match_all('#\[\[(\w+)\]\]#u', $string, $name);
preg_match_all('/style=(\'|\")([ -0-9a-zA-Z:]*[ 0-9a-zA-Z;]*)*(\'|\")/', $string, $name2);
$i = 0;
foreach ($name[1] as $index => $value) {
echo '<br/>' . $value, $name2[$index];
}
}
}
displayTXTList('smiley2.txt');
这是我得到的:
sadArray
cryingArray
sunArray
cloudArray
raining
coffee
cute_happy
snowman
sparkle
heart
lightning
sorry
so_sorry
etc...
但我想要这个:
sadstyle='background-position: -0px -0px;'
cryingstyle='background-position: -16px -0px;'
sunstyle='background-position: -32px -0px;'
etc...
实际的txt文件是这样的:
[[sad]]<span class='smiley' style='background-position: -0px -0px;'></span>
[[crying]]<span class='smiley' style='background-position: -16px -0px;'></span>
[[sun]]<span class='smiley' style='background-position: -32px -0px;'></span>
[[cloud]]<span class='smiley' style='background-position: -48px -0px;'></span>
[[raining]]<span class='smiley' style='background-position: -64px -0px;'></span>
etc...
我怎么能做到这一点?我是新来的,所以请不要记下:/