Jump to content

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


larry29936

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

Link to comment
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 "/"?

Link to comment
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.

Link to comment
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,'/');

?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.