Jump to content
#StayAtHome ×
larry29936

How to extract a given number of characters from a php variable

Recommended Posts

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

Share this post


Link to post
Share on other sites

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 "/"?

Share this post


Link to post
Share on other sites
Posted (edited)

Is that all in one string?  As in a variable?  Read up on str_replace perhaps?

Edited by ginerjm

Share this post


Link to post
Share on other sites

@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 "/".

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites

Problem resolved.

<?php
                   $files = array();
                    $files = glob('download/*.iso');
                    $file = $files[count($files) - 1];
                    $info = pathinfo($file);
                    $filename =  basename($file);
                    $filename = ltrim($filename,'/');

?>

 

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.