我的问题是它只是重复了两次这个数字,虽然它在有一个数字的时候增加了断续,但是我试图检查这个数字后面是否有一个数字,所以它会说第12行是……
谢谢你的帮助
<?PHP
$lines = file_get_contents('http://www.webstitcher.com/test.txt');
$tag = str_split($lines); // puts all lines into a array
foreach ($tag as $num => $letta){
if (is_numeric($letta) == TRUE){
$num2 = $num++;
if (is_numeric($tag[$num2])){ // checks if next line is going to be another digit
$letta .= $tag[$num2];
unset($tag[$num2]); // removes line if it had another digit and adds to ouput
}
echo '<br />' . $letta;
}
else {
echo $letta;
}
}
?>发布于 2017-10-06 00:53:53
尝试使用' '作为分隔符爆炸字符串。这将使您能够保持整个数字,并最终将有助于减少大量的复杂性。
$lines = file_get_contents('http://www.webstitcher.com/test.txt');
$tag = explode(' ', $lines); // puts all words into a array
foreach ($tag as $word){
if (is_numeric($word)) {
// if the word is numeric, simply skip to next line
// if you need to keep the number, add $word to the echo statement
echo '<br />';
}
else {
echo ' '.$word;
}
}这样,您就不必跟踪数组中的前一个元素,也不必检查下一个元素。
或者,您也可以使用preg_replace,这将完全消除对循环的需求。
$lines = preg_replace('/[0-9]+/', '<br>', $words);https://stackoverflow.com/questions/46596487
复制相似问题