Page 1 of 1

Crop the four ending characters from a string?

Posted: Mon Dec 22, 2008 4:48 pm
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.

Re: Crop the four ending characters from a string?

Posted: Mon Dec 22, 2008 5:01 pm
by jaoudestudios
Duuuude!

substr :P

Re: Crop the four ending characters from a string?

Posted: Mon Dec 22, 2008 5:07 pm
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.

Re: Crop the four ending characters from a string?

Posted: Mon Dec 22, 2008 5:08 pm
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);
?>

Re: Crop the four ending characters from a string?

Posted: Mon Dec 22, 2008 5:08 pm
by JAB Creations

Re: Crop the four ending characters from a string?

Posted: Mon Dec 22, 2008 5:11 pm
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);
?>

Re: Crop the four ending characters from a string?

Posted: Mon Dec 22, 2008 5:31 pm
by John Cartwright
Theres also

Code: Select all

echo basename('members.php', '.php');

Re: Crop the four ending characters from a string?

Posted: Mon Dec 22, 2008 5:40 pm
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:

Re: Crop the four ending characters from a string?

Posted: Mon Dec 22, 2008 7:40 pm
by kaisellgren
How about strrev() then strpos() to get the position of the dot and lastly substr() ?

EDIT: and strrev() back :D

Re: Crop the four ending characters from a string?

Posted: Mon Dec 22, 2008 7:48 pm
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:

Re: Crop the four ending characters from a string?

Posted: Mon Dec 22, 2008 7:58 pm
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

Re: Crop the four ending characters from a string?

Posted: Tue Dec 23, 2008 10:56 pm
by Syntac
You could do a <?php echo strrev(ob_get_clean()); ?>, but that would destroy everything. :?