Get numbers between comma in string in php

I have the following line:

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

I am assigned a number, and I must find the next next and previous number for it.

For example, if I was given 14, then the next number would be NULL, and the previous one would be 13. If I was given 8, then the previous one would be 7, and the next one would be 9. If I get 1, then the previous one would be null and the next one would be 2.

I have the following code, but it does not work for numbers greater than 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]);
}

Can someone tell me what I am doing wrong? How to fix it? PS: This is a homework question, and I'm not allowed to use explode (). Use only string manipulation functions. Thanks

+4
source share
4 answers

?

<?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 | 

edit: ( ) ( , , , , - ). - " " - , ? $page "abc, , , xyz"? ( , script , ).

<?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

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]);
    }
0
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));
-1
source

This will work for you.

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;
}
-1
source

All Articles