larry29936 Posted March 19, 2020 Share Posted March 19, 2020 I have the following code in my index.php: <?php $files = array(); $files = glob('download/*.iso'); $file = $files[count($files) - 1]; $strlen ( string $file ) : int /* length of $file */ $filelen = $strlen -9 /* length of unwanted characters - "download/" */ $filename = /* how do I get the filename starting at the 10th position */ ?> <div class="container"> <div class="divL"> <h3>Get the latest version of FoxClone iso</h3> <a href="<?php echo "/{$file}";?>"><img src="images/button_get-the-app.png" alt="" width="200" height="60"></a> I'd like to replace "Get the latest version of FoxClone iso" in the next to last line with "Get Foxclone "<?php echo "/{$filename}";?>". How do I extract just the file name from $file? I know that I have to: 1. get the length of $file 2. subtract the number of unwanted character to determine the start character (In this case, start at 10th character) 3. and extract everything from the 10th character to length of $file. Step 3 is where I just don't know how to accomplish the task. I'd appreciate some help on this. Thanks in advance, Larry Quote Link to comment https://forums.phpfreaks.com/topic/310329-how-to-extract-a-given-number-of-characters-from-a-php-variable/ Share on other sites More sharing options...
requinix Posted March 19, 2020 Share Posted March 19, 2020 Actually the process needs to be: 1. Use basename Quote Link to comment https://forums.phpfreaks.com/topic/310329-how-to-extract-a-given-number-of-characters-from-a-php-variable/#findComment-1575624 Share on other sites More sharing options...
larry29936 Posted March 20, 2020 Author Share Posted March 20, 2020 I tried the following: <?php $files = array(); $files = glob('download/*.iso'); $file = $files[count($files) - 1]; $info = pathinfo($file); $filename = basename($file,'.'.$info['extension']); ?> It is giving me "Get Foxclone "/foxclone30-1"". How do I get rid of the leading "/"? Quote Link to comment https://forums.phpfreaks.com/topic/310329-how-to-extract-a-given-number-of-characters-from-a-php-variable/#findComment-1575626 Share on other sites More sharing options...
ginerjm Posted March 20, 2020 Share Posted March 20, 2020 (edited) Is that all in one string? As in a variable? Read up on str_replace perhaps? Edited March 20, 2020 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/310329-how-to-extract-a-given-number-of-characters-from-a-php-variable/#findComment-1575627 Share on other sites More sharing options...
requinix Posted March 20, 2020 Share Posted March 20, 2020 Oh, you don't want the extension either? Look at the documentation for pathinfo. The answer is there. One single function call is all it will take. Quote Link to comment https://forums.phpfreaks.com/topic/310329-how-to-extract-a-given-number-of-characters-from-a-php-variable/#findComment-1575629 Share on other sites More sharing options...
larry29936 Posted March 20, 2020 Author Share Posted March 20, 2020 @requinix - I do want the file extension and I should be getting it with the code that I have. I just don't want the leading "/". Quote Link to comment https://forums.phpfreaks.com/topic/310329-how-to-extract-a-given-number-of-characters-from-a-php-variable/#findComment-1575630 Share on other sites More sharing options...
requinix Posted March 20, 2020 Share Posted March 20, 2020 Okay... I said that because the code you wrote was trying to remove the extension. Either basename() or pathinfo(). Either of them in a single function call will give you the value you need. Directly. You don't have to care about the slash. Please just read the documentation for one of them. Quote Link to comment https://forums.phpfreaks.com/topic/310329-how-to-extract-a-given-number-of-characters-from-a-php-variable/#findComment-1575631 Share on other sites More sharing options...
larry29936 Posted March 20, 2020 Author Share Posted March 20, 2020 Problem resolved. <?php $files = array(); $files = glob('download/*.iso'); $file = $files[count($files) - 1]; $info = pathinfo($file); $filename = basename($file); $filename = ltrim($filename,'/'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/310329-how-to-extract-a-given-number-of-characters-from-a-php-variable/#findComment-1575632 Share on other sites More sharing options...
ginerjm Posted March 20, 2020 Share Posted March 20, 2020 Very well done. Now a bit of learning. You don't need to declare $files as an array since the result of the glob call IS an array! Quote Link to comment https://forums.phpfreaks.com/topic/310329-how-to-extract-a-given-number-of-characters-from-a-php-variable/#findComment-1575638 Share on other sites More sharing options...
requinix Posted March 20, 2020 Share Posted March 20, 2020 10 hours ago, larry29936 said: $filename = ltrim($filename,'/'); basename() will not return the slash. You're obsessing about it perhaps a little bit too much. Quote Link to comment https://forums.phpfreaks.com/topic/310329-how-to-extract-a-given-number-of-characters-from-a-php-variable/#findComment-1575640 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.