首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP在数组中找到两个连续的数字条目

PHP在数组中找到两个连续的数字条目
EN

Stack Overflow用户
提问于 2017-10-06 00:09:19
回答 1查看 63关注 0票数 0

我的问题是它只是重复了两次这个数字,虽然它在有一个数字的时候增加了断续,但是我试图检查这个数字后面是否有一个数字,所以它会说第12行是……

谢谢你的帮助

代码语言:javascript
复制
<?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;
        }
}

?>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-10-06 00:53:53

尝试使用' '作为分隔符爆炸字符串。这将使您能够保持整个数字,并最终将有助于减少大量的复杂性。

代码语言:javascript
复制
$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,这将完全消除对循环的需求。

代码语言:javascript
复制
$lines = preg_replace('/[0-9]+/', '<br>', $words);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46596487

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档