How do you split up a filename in PHP, and insert code?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do you split up a filename in PHP, and insert code?

Post by simonmlewis »

We are looking at using srcset in our website, writing it ourselves.

Is it best to store each version name in it’s own database field? image, image475, image768 and so on?
Or a better way I was thinking (which is similar perhaps to Wordpress) is to dynamically write on page what numbers we want.

So :

Code: Select all

<img srcset="/images/pages/$row->image”.475.”.jpg 475w,
             /images/pages/$row->image”.768.”jpg 768w,
             /images/pages/$row->image”.475.”.jpg 1920w"
     sizes="(max-width: 475px) 475px,
            (max-width: 768px) 768px,
            (max-width: 1920px) 1920px"
     src="/images/pages/25644881item-phone-name.jpg" alt='photo name here'>
$row->image”.475.”
My question is, how do I break up the filename from the variable (in case we did a .jpeg or a .png), and concatenate in that file extension?

So we take $row->image, split up the name and extension into two variables. So it reads a little more like this:

Code: Select all

<img srcset="/images/pages/$imagename"."475"."$imageextension"." 475w,....
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you split up a filename in PHP, and insert code?

Post by Celauran »

There seems to be quite a lot going on in that question, so let me start with this:
simonmlewis wrote:My question is, how do I break up the filename from the variable (in case we did a .jpeg or a .png), and concatenate in that file extension?

So we take $row->image, split up the name and extension into two variables. So it reads a little more like this:

Code: Select all

<img srcset="/images/pages/$imagename"."475"."$imageextension"." 475w,....
pathinfo
Specifically, see the first example.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you split up a filename in PHP, and insert code?

Post by simonmlewis »

Yes path info is perfect. Works well. Thanks.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply