在php

时间:2016-02-24 10:09:25

标签: php string

我有以下字符串:

 $pages="1,2,3,4,5,6,7,8,9,10,11,12,13,14";

我收到了一个号码,我必须找到下一个和之前的号码。

例如,如果我被给予14,则下一个数字将为NULL,而之前的数字将为13。  如果我给了8,那么之前将是7,接下来将是9。  如果我得到1,那么之前将为null,接下来将是2.

我有以下代码,但它不适用于大于9的数字:

function getPages($number = 5)
 {
        $pages="1,2,3,4,5,6,7,8,9,10,11,12,13,14";
        $x = strpos($pages, $number);

        if($x == 0)
          return array('prev' => null, 'next' => $pages[$x]);
        else if($x == (strlen($pages) - 1))
          return array('prev' => $pages[$x - 2], 'next' => null);
        else
             return array('prev' => $pages[$x - 2], 'next' => $pages[$x + 2]);
}

谁能告诉我我做错了什么?如何解决?  P.S:这是家庭作业问题,我不允许使用explode()。仅使用字符串操作函数。  感谢

4 个答案:

答案 0 :(得分:1)

你是否被允许使用正则表达式?

<?php
$pages="1,2,3,4,5,6,7,8,9,10,11,12,13,14";

function foo($x, $pages) {
    $pattern = '!
        (^|\d+,)
        '.$x.'
        ($|,\d+)
    !x';

    echo $x, ' | ';
    if ( preg_match($pattern, $pages, $c) ) {
        echo 'prev: ', substr($c[1], 0, -1), ' | ';
        echo 'next: ', substr($c[2], 1);
    }
    echo "\r\n";
}

for($x=0; $x<16; $x++) {
    foo($x, $pages);
}

打印

0 | 
1 | prev:  | next: 2
2 | prev: 1 | next: 3
3 | prev: 2 | next: 4
4 | prev: 3 | next: 5
5 | prev: 4 | next: 6
6 | prev: 5 | next: 7
7 | prev: 6 | next: 8
8 | prev: 7 | next: 9
9 | prev: 8 | next: 10
10 | prev: 9 | next: 11
11 | prev: 10 | next: 12
12 | prev: 11 | next: 13
13 | prev: 12 | next: 14
14 | prev: 13 | next: 
15 | 

编辑:第二个想法(午餐时间)这听起来非常像家庭作业(所以,请不要只是复制和粘贴它,但要尝试理解它并询问是否有不清楚的事情)。问题标题是“在逗号字符之间获取数字” - 但这实际上是给你的任务吗?或者$页面还包含“abc,热核,热影,xyz”? (如果我是你的导师,那就是我要测试你的剧本的内容)。

<?php
$pages= array(
    'abc',
    'thermonuclear',
    'the',
    'thermopylae',
    'xyz'
);


function foo($x, $pages, $offset=0) {
    $start = strpos($pages, (string)$x, $offset);
    if ( false===$start ) {
        return false;
    }
    else {
        $end = $start + strlen($x);
        // the pattern found must be 
        // a) either at the start of the string or preceeded by a comma
        // and b) the string must end with the pattern or the next character is a comma

        if (
            (0!==$start && ','!==$pages[$start-1])
            || ( $end!==strlen($pages) && ','!==$pages[$end])
        ) {
            // pattern was not complete
            // continue search
            return foo($x, $pages, $offset+strlen($x));
        }
        else {
            return array(
                'previous'=>previousElement($pages, $start),
                'current'=>$x,
                'next'=>nextElement($pages, $end)
            );
        }
    }
}

function previousElement($str, $offset) {
    if ( $offset < 1 ) {
        return NULL;
    }
    else {
        $offset-=2;
        $tmp = strrpos($str, ',', -(strlen($str)-$offset));
        return false===$tmp ? substr($str, 0, $offset+1) : substr($str, $tmp+1, $offset-$tmp);
    } 
}

function nextElement($str, $offset) {
    if ( $offset >= strlen($str) ) {
        return NULL;
    }
    else {
        $tmp = strpos($str, ',', $offset+1);
        return false===$tmp ? substr($str, $offset+1) : substr($str, $offset+1, $tmp-$offset);
    }
}

echo join(',', $pages), "\r\n";
foreach($pages as $p) {
    var_export( foo($p, join(',', $pages)) );
    echo "\r\n";
}

答案 1 :(得分:0)

在参数中将5作为字符串传递并检查。目前它被视为整数。请参阅以下代码。

function getPages($number = "5")
     {
            $pages="1,2,3,4,5,6,7,8,9,10,11,12,13,14";

            $x = strpos($pages, $number);

            if($x == 0)
              return array('prev' => null, 'next' => $pages[$x]);
            else if($x == (strlen($pages) - 1))
              return array('prev' => $pages[$x - 2], 'next' => null);
            else
             return array('prev' => $pages[$x - 2], 'next' => $pages[$x + 2]);
    }

答案 2 :(得分:-1)

function getPages($number = 5){
    $pages="1,2,3,4,5,6,7,8,9,10,11,12,13,14";
    $page_array = explode( ',' , $pages);
    if($number == 0):
        return array('prev' => null , 'next' => $page_array[$number]);
    elseif($number == count($page_array)): 
        return array('prev' => $page_array[count($page_array)-2] , 'next' => null);
    elseif($number > count($page_array) || $number < 0):
        return false;
    else:
        echo 'from else part';
        return array('prev' => $page_array[$number-2] , 'next' => $page_array[$number]);
    endif;
}

//test function
echo 'For 0 th item: '; 
    print_r( getPages(0));
echo '<br>For 14 th item: ';    
    print_r( getPages(14));
echo '<br>For 4 th item: ';    
    print_r( getPages(4));
echo '<br>For 15 th item: ';   
    print_r( getPages(15));

答案 3 :(得分:-1)

这对你有用..

function getPages($number = 5)
{
$pagesList="1,2,3,4,5,6,7,8,9,10,11,12,13,14";
$pages = explode(',',$pagesList);
$count = count($pages);
$x = array_search($number,$pages);
if($x == NULL)
    $result= array('prev' => null, 'next' => null);
else if($x == 0)      
    $result= array('prev' => null, 'next' => $pages[$x]);               
else if($x == ($count - 1))
    $result= array('prev' => $pages[$count - 2], 'next' => null);
else
    $result= array('prev' => $pages[$x-1], 'next' => $pages[$x+1]);      
return $result;
}