[英]PHP Find two consecutive numeric entries in array
我的问题是它只是将数字复制两次,尽管在有数字时确实会添加中断,但我正在尝试检查数字后是否有数字,因此它会说第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;
}
}
?>
尝试使用' '
作为分隔符来分解字符串。 这将使您能够保持完整的数字,并最终帮助减少很多复杂性。
$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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.