Hey, array_shift seems to work on another server, but it does not on mine. The code is the following; $realid = array_shift(explode("-", $id)); PHP: 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? $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]); PHP: Is there an another way of doing this, without using array_shift? Thanks,
<?php $arrays = array( 'example.com/48555-animal-name.html', 'example.com/48555-345-animal-na3me.html', '48555-animal-name.html' ); foreach ($arrays as $item): preg_match('/.*\/?([0-9]+)-/U', $item, $res); echo $res[1]; endforeach; PHP:
That really does look like you are doing it the hard way. I'd try something like this, takes a few more steps but is readable and debugable. $url = "example.com/48555-animal-name.html"; $pos = 1; // how many / are expected before the id $bits =explode('/', $url); $idbits = explode('-',$bit[$pos]); $id = $idbits[0]; PHP: edit: ThePHPMaster is onto it, preg is faster and allows subtle changes as per the example