Printable Version of Topic

Click here to view this topic in its original format

HTMLHelp Forums _ Server-side Scripting _ How to extract a given number of characters from a php variable

Posted by: larry78723 Mar 19 2020, 04:37 PM

I have the following code in my index.php:

CODE

                   <?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 in $file  */
                   ?>

                     <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.

I think steps 1 and 2 are correct but 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

Posted by: CharlesEF Mar 19 2020, 07:45 PM

Umm, it seems you just copied the strlen command from the manual.

CODE
$strlen ( string $file ) : int  /* length of $file */
The manual is telling you that strlen accepts a string parameter and returns an int. So, you should use it like this:
CODE
$len = strlen(file);

The string you want to replace, is that located in the H3 tag? If yes, then you can't do that with PHP. As your code is written right now.

Posted by: larry78723 Mar 19 2020, 08:04 PM

QUOTE(CharlesEF @ Mar 19 2020, 08:45 PM) *

Umm, it seems you just copied the strlen command from the manual.
CODE
$strlen ( string $file ) : int  /* length of $file */
The manual is telling you that strlen accepts a string parameter and returns an int. So, you should use it like this:
CODE
$len = strlen(file);

The string you want to replace, is that located in the H3 tag? If yes, then you can't do that with PHP. As your code is written right now.

Charles, based on research I've changed the way I'm doing it:

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

This yields "Get Foxclone /foxclone30-1" in the h3 tag. Now I just need to figure out how to get rid of the leading "/" and why it's not showing the extension.


Posted by: CharlesEF Mar 19 2020, 09:23 PM

If you want the file extension then why cut it off?:

CODE
$filename = basename($file,'.'.$info['extension']);
Remove the 2nd parameter and you will get the extension also.
To remove the 1st character look at 'ltrim'.

Posted by: larry78723 Mar 19 2020, 10:42 PM

Thanks Charles, that took care of it.

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)