seularts Posted May 10, 2008 Share Posted May 10, 2008 How do i explode a path such as: c:/path1/path2/path3/file.exe, by counting the words(because the path can vary) in the path and exploding them through "/", and then exploding again the last 2 words delimited through "." remaining only with the word file. Can anyone help me with this!? Quote Link to comment https://forums.phpfreaks.com/topic/105016-explode-function/ Share on other sites More sharing options...
AndyB Posted May 10, 2008 Share Posted May 10, 2008 Show us your code so far, then we'll help. Quote Link to comment https://forums.phpfreaks.com/topic/105016-explode-function/#findComment-537524 Share on other sites More sharing options...
seularts Posted May 10, 2008 Author Share Posted May 10, 2008 <? $str = "c:/etc/asd/ada/fda.jpg"; $primu= explode("/",$str,5); $doi=explode(".",$primu[4]); echo $doi[0]; ?> The result is the word "fda", but here i`m counting the word manualy, i would like something like a loop for the arry count of the words, if that is possible:) Quote Link to comment https://forums.phpfreaks.com/topic/105016-explode-function/#findComment-537532 Share on other sites More sharing options...
Zane Posted May 10, 2008 Share Posted May 10, 2008 you could use regex.... $filename = ereg("/(.*)\..{3,4}", $str, $arr) ? $arr[1] : "No File Name"; haven't tested this code Quote Link to comment https://forums.phpfreaks.com/topic/105016-explode-function/#findComment-537537 Share on other sites More sharing options...
seularts Posted May 10, 2008 Author Share Posted May 10, 2008 this is the output: etc/asd/ada/fda, so i still remain with the problem of selecting the last word from the count. because the path could vary from 5 to even 20 words.. so there wouldn't be any hope to select thw eord bi hand imput, so i do need a loop Quote Link to comment https://forums.phpfreaks.com/topic/105016-explode-function/#findComment-537541 Share on other sites More sharing options...
Zane Posted May 10, 2008 Share Posted May 10, 2008 here try this $filename = ereg("/(.*\..{3,4})"), $str, $arr) ? $arr[1] : "No File Name"; you shouldn't need a loop if you just need the filename like that. Regex can chop it all up no matter how long it is EDIT: I had to edit the code btw Quote Link to comment https://forums.phpfreaks.com/topic/105016-explode-function/#findComment-537544 Share on other sites More sharing options...
seularts Posted May 10, 2008 Author Share Posted May 10, 2008 there is no output this way, the page just gows blank:)) Quote Link to comment https://forums.phpfreaks.com/topic/105016-explode-function/#findComment-537545 Share on other sites More sharing options...
Zane Posted May 10, 2008 Share Posted May 10, 2008 there's always this $filename = substr($str,strrpos($str,"/"),0); Quote Link to comment https://forums.phpfreaks.com/topic/105016-explode-function/#findComment-537551 Share on other sites More sharing options...
seularts Posted May 10, 2008 Author Share Posted May 10, 2008 same result.. blank page.. Quote Link to comment https://forums.phpfreaks.com/topic/105016-explode-function/#findComment-537555 Share on other sites More sharing options...
LemonInflux Posted May 10, 2008 Share Posted May 10, 2008 <?php $path = 'c:/path1/path2/path3/file.exe'; $boom = explode("/", $path); $lolExplosion = explode(".", end($boom)); echo $lolExplosion[0]; ?> Quote Link to comment https://forums.phpfreaks.com/topic/105016-explode-function/#findComment-537556 Share on other sites More sharing options...
LemonInflux Posted May 10, 2008 Share Posted May 10, 2008 Sorry, fixed the code above. ^ Tested, and works. Outputs 'file' Quote Link to comment https://forums.phpfreaks.com/topic/105016-explode-function/#findComment-537559 Share on other sites More sharing options...
seularts Posted May 10, 2008 Author Share Posted May 10, 2008 wow.. thanks alot man.. that work perfectly:D this was the last piece of the puzzle for me at my console:P Quote Link to comment https://forums.phpfreaks.com/topic/105016-explode-function/#findComment-537566 Share on other sites More sharing options...
LemonInflux Posted May 10, 2008 Share Posted May 10, 2008 haha no prob. I'd never found a reason to use the end() function before, so thanks for posting this Quote Link to comment https://forums.phpfreaks.com/topic/105016-explode-function/#findComment-537567 Share on other sites More sharing options...
papaface Posted May 10, 2008 Share Posted May 10, 2008 Or you could use: <?php $path = 'c:/path1/path2/path3/file.exe'; $info = pathinfo($path); echo $info['filename']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/105016-explode-function/#findComment-537593 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.