Results 1 to 3 of 3

Thread: An Alternative to array_shift and explode? - Please Help

  1. #1
    5starpix's Avatar
    5starpix is offline Senior Net Builder
    Join Date
    Dec 2008
    Location
    Montreal, Quebec
    Posts
    1,710
    Blog Entries
    9
    Thanks
    158
    Thanked 227 Times in 164 Posts

    Question An Alternative to array_shift and explode? - Please Help

    Hey,

    array_shift seems to work on another server, but it does not on mine.

    The code is the following;

    PHP Code:
    $realid array_shift(explode("-"$id)); 
    What I am trying to get done is the following;

    I have a URL, which is like [ example.com/48555-animal-name.html ]. How is it possible to ONLY get the ID which is 48555, to call an entry from the database?

    PHP Code:
    $realid array_shift(explode("-"$id));
    $qryrec="select * from animals where id='$realid'";
    $resultrec=mysql_query($qryrec) or die($qryrec);
    $linerec=mysql_fetch_array($resultrec);
    $pcommon ucwords($linerec[common]); 
    Is there an another way of doing this, without using array_shift?

    Thanks,

  2. #2
    Andy101's Avatar
    Andy101 is offline Code Otaku
    Join Date
    Aug 2009
    Location
    Kanazawa
    Posts
    1,238
    Thanks
    171
    Thanked 303 Times in 230 Posts
    Your example should work if $id = "48555-animal-name.html" since array_shift is available in PHP 4 and PHP 5.

    I suspect that the value stored in $id has something before the id number.

    This should work as long as the id is the only part of the url containing numbers:
    PHP Code:
    $realid preg_replace('@[^0-9]@'''$id); 

  3. #3
    5starpix's Avatar
    5starpix is offline Senior Net Builder
    Join Date
    Dec 2008
    Location
    Montreal, Quebec
    Posts
    1,710
    Blog Entries
    9
    Thanks
    158
    Thanked 227 Times in 164 Posts
    Great! - Thanks buddy

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •