The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> How to extract a given number of characters from a php variable
larry78723
post Mar 19 2020, 04:37 PM
Post #1


Member
***

Group: Members
Posts: 33
Joined: 5-March 20
Member No.: 27,220



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

This post has been edited by larry78723: Mar 19 2020, 04:40 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Mar 19 2020, 07:45 PM
Post #2


Programming Fanatic
********

Group: Members
Posts: 1,787
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



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.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
larry78723
post Mar 19 2020, 08:04 PM
Post #3


Member
***

Group: Members
Posts: 33
Joined: 5-March 20
Member No.: 27,220



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.

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
CharlesEF
post Mar 19 2020, 09:23 PM
Post #4


Programming Fanatic
********

Group: Members
Posts: 1,787
Joined: 27-April 13
From: Edinburg, Texas
Member No.: 19,088



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

This post has been edited by CharlesEF: Mar 19 2020, 09:27 PM
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
larry78723
post Mar 19 2020, 10:42 PM
Post #5


Member
***

Group: Members
Posts: 33
Joined: 5-March 20
Member No.: 27,220



Thanks Charles, that took care of it.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 30th October 2020 - 12:10 PM