Crop the four ending characters from a string?

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
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Crop the four ending characters from a string?

Post by JAB Creations »

I forget which string related function it was and I'm not seeing it looking at php.net's string functions?

Let's say I have 'members.php', I simply want to crop off the last four characters and save the string as 'members'. I'm not looking for any characters/no regex of any type, just want to crop off the last four characters.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Crop the four ending characters from a string?

Post by jaoudestudios »

Duuuude!

substr :P
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Crop the four ending characters from a string?

Post by JAB Creations »

I know I know, the problem was the file names vary in length.

I thought there was another string function that combined to do this...

Code: Select all

<?php
$text = 'members.php';
$length = strlen($text);
$crop = $length -4;
 
echo substr($text, 0, $crop);
?>
I am going to search for an earlier thread to see if I can find what I have in the back of my mind though...I'm pretty certain this was mentioned here on the forums in a thread I had participated in before at one point.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: Crop the four ending characters from a string?

Post by Mark Baker »

Code: Select all

<?php
$text = 'members.php';
 
echo substr($text, 0, -4);
?>
or

Code: Select all

<?php
$text = 'members.php';
 
echo pathinfo($text,PATHINFO_FILENAME);
?>
Last edited by Mark Baker on Mon Dec 22, 2008 5:10 pm, edited 1 time in total.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Crop the four ending characters from a string?

Post by JAB Creations »

User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Crop the four ending characters from a string?

Post by JAB Creations »

Ok not sure why I didn't see that originally! I must have a lot of brain matter to always constantly be loosing my mind! :mrgreen:

Code: Select all

<?php
$text1 = 'members.php';
echo substr($text1, 0, -4);
 
echo '<br /><br />';
 
$text2 = 'members123.php';
echo substr($text2, 0, -4);
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Crop the four ending characters from a string?

Post by John Cartwright »

Theres also

Code: Select all

echo basename('members.php', '.php');
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Crop the four ending characters from a string?

Post by JAB Creations »

I also forgot about basename Jcart however I'm doing the following to determine the full location hierarchy...

Code: Select all

$pieces1 = explode("/", $_SERVER['PHP_SELF']);
 
if ($_SERVER['HTTP_HOST'] != "localhost") {$section = $pieces1[1]; $file = $pieces1[2];}
else if ($_SERVER['HTTP_HOST'] == "localhost") {$section = $pieces1[2]; $file = $pieces1[3];}
I'm working on the #location part of my site to dynamically show the location hierarchy. I'm setting this to a class (the SEO layout makes this difficult to do without a class)...

Code: Select all

$location2 = '<a href="'.$section.'/'.$file.'" tabindex="2" title="'.ucfirst(substr($file, 0, -4)).'">'.ucfirst(substr($file, 0, -4)).'</a>';
Image

The #location is where #2 in the image is displayed. It's currently dynamically handling the forum location beautifully. :)

I'm making the next version completely database driven as this is forcing me to learn databases to the extent I hope would make me much more qualified for future jobs.

The title is generated for the forums though for regular pages on my site it'll be pulled from the database as well.

#3 on the image is a nifty class I can change to put the mini-sidebar per post on either left or right by simply having PHP change the class. That'll be a user preference. Also CSS3 rounded corners! (Currently supported by Gecko and Webkit only though)

#4 shows how the text wraps around the mini-sidebar. It does not however wrap around when the post text is displayed in multiple columns (currently only supported by Gecko and Webkit).

Right now I'm trying to make sure the whole site will be rendered WAI AAA compliant and as application/xhtml+xml...oh and the layout will work decently in IE5 and Opera 6 (though require some patching but it can be done).

I'm going to update jabcreations.net before the end of the year with this stuff once I have things to the point where it would be sane to put up a working preview for the general public.

Oh and I'm learning stuff...and recreating neural connections for things that have recently slipped my mind! :wink:
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Crop the four ending characters from a string?

Post by kaisellgren »

How about strrev() then strpos() to get the position of the dot and lastly substr() ?

EDIT: and strrev() back :D
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Crop the four ending characters from a string?

Post by JAB Creations »

That's a creative idea though Mark shared how the way I desired to use the substr function...the only way I hadn't tried. :roll:

Though now that you've mentioned it I'm going to have to find a way to apply strrev to my entire website for April 1st. :mrgreen:
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Re: Crop the four ending characters from a string?

Post by kaisellgren »

JAB Creations wrote:That's a creative idea though Mark shared how the way I desired to use the substr function...the only way I hadn't tried. :roll:

Though now that you've mentioned it I'm going to have to find a way to apply strrev to my entire website for April 1st. :mrgreen:
Hmm... so you have 2 weeks to do it, hurry! :D
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: Crop the four ending characters from a string?

Post by Syntac »

You could do a <?php echo strrev(ob_get_clean()); ?>, but that would destroy everything. :?
Post Reply