PHP explode last element

  • seularts
  • Graduate
  • Graduate
  • User avatar
  • Posts: 153
  • Loc: Romania

Post 3+ Months Ago

How can I explode an element that looks like this one.two.three.four.five and only eliminate the last element in this case five, but keeping in mind that five is a variable that changes, so just exploding .five won't do it. Is there any way to do the explode in reverse (from five to one, removing only five) instead from one to five!?
Moderator Remark: Title correction
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Can you post a few examples of what you would be left with ?

I see your input would be something like this

Code: [ Select ]
$str = 'one.two.three.four.five';


Would you want this returned ?

Code: [ Select ]
$result = 'one.two.three.four';


Maybe this ?

Code: [ Select ]
$result = array('one', 'two', 'three', 'four');


Or this ?

Code: [ Select ]
$result = array('one.two.three.four', '.five');
  • seularts
  • Graduate
  • Graduate
  • User avatar
  • Posts: 153
  • Loc: Romania

Post 3+ Months Ago

I fixed it like this:
Code: [ Select ]
        $break=explode('.', $img);
        $pfile = $break[count($break) - 1];
        $lf=explode('.'.$pfile, $img);
        $exp=$lf[0];
  1.         $break=explode('.', $img);
  2.         $pfile = $break[count($break) - 1];
  3.         $lf=explode('.'.$pfile, $img);
  4.         $exp=$lf[0];


, where $img could be a .gif .jpg or .png file. So despite the name of the picture, for example picture.xxx.online.after.jpg, I would only like to remove the extension. So in a loop the extension of the picture, whatever it is, is removed, and I can use the name to do what I want afterwords with it! In my case i used it to make a .php file with the same name as the .jpg name to suite the needs of my script.
  • joebert
  • Fart Bubbles
  • Genius
  • User avatar
  • Posts: 13502
  • Loc: Florida

Post 3+ Months Ago

Code: [ Select ]
$result = substr($img, 0, strrpos($img, '.'))


If you have PHP 5.2+ available you can use the PATHINFO_FILENAME constant with pathinfo.

Code: [ Select ]
$result = path_info($img, PATHINFO_FILENAME);
  • seularts
  • Graduate
  • Graduate
  • User avatar
  • Posts: 153
  • Loc: Romania

Post 3+ Months Ago

Thanks :)

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: ScottG and 109 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.