chopping 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
macewan
Forum Commoner
Posts: 97
Joined: Mon Jul 07, 2003 8:27 pm

chopping from a string

Post by macewan »

don't laugh :?

how would i chop off the last four characters of a string?

12345678

into

1234
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

$chopped = substr($the_string, 0, -4);
oraknabo
Forum Newbie
Posts: 14
Joined: Mon Oct 07, 2002 5:48 pm

Post by oraknabo »

Code: Select all

<?php
$newstring = substr($oldstring, 0, 4);
?>
-4 would give you "5678"
Whenever I want to find out how to do anything with a string I don't already know, I look here first.
macewan
Forum Commoner
Posts: 97
Joined: Mon Jul 07, 2003 8:27 pm

Post by macewan »

thanks guys

i want to chop off the ".jpg" from http://www.macewan.org/screenshots/

grabs images that are uploaded to the folder & displays their name minus the .jpg



///

<body bgcolor="#ffffff" text="#000000">
<blockquote>
<?php
/*
* This little example prints a sorted directory
* listing, with dirs first and files afterwards.
*/

// Open current directory
if($handle = opendir("./")){

// Loop through all files
while(false !== ($file = readdir($handle))){

// Ignore hidden files
if(!preg_match("/^\./", $file)){

// Put dirs in $dirs[] and files in $files[]
if(is_dir($file)){
$dirs[] = $file;
}else{
$files[] = $file;
}
}
}

// Close directory
closedir($handle);

// if $dirs[] exists, sort it and print all elements in it.
if(is_array($dirs)){
sort($dirs);
foreach($dirs as $dir){
echo "<a href=\"$dir/\">$dir/</a><br />\n";
}
}

// if $files[] exists, sort it and print all elements in it.
if(is_array($files)){
sort($files);
foreach($files as $file){
$larry = substr($file, 0, -4);
switch($file)
{
case "index.php";
break;
default:
echo "<a href=\"$file\">$larry</a><br />\n";
}
}
}
}
?>
</blockquote>
</body>
</html>
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

you don't need to chop it off... you can simply replace the bit you don't want (ie .jpg) with erm.. nothing.

Code: Select all

<?php
$file_name="image.jpg";
$without_extension = str_replace(".jpg","",$file_name);
?>
... if you wanted to replace .jpg with something like BOB ...

Code: Select all

<?php
$file_name="image.jpg";
$new_string = str_replace(".jpg","BOB",$file_name);
?>
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

oraknabo wrote:

Code: Select all

<?php
$newstring = substr($oldstring, 0, 4);
?>
-4 would give you "5678"
Whenever I want to find out how to do anything with a string I don't already know, I look here first.

You're so wrong dude...... substr($string, 0, 4) would give you the first 4 characters, not chop four characters off the end. substr($string, 0, -4) will chop four characters off the end of the string.... check the manual.
macewan
Forum Commoner
Posts: 97
Joined: Mon Jul 07, 2003 8:27 pm

Post by macewan »

would

Code: Select all

<?php
 $larry = substr($file, 0, -4); 
?>
really be that much overhead compared to

Code: Select all

<?php
$without_extension = str_replace(".jpg","",$file_name); 

?>

?
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post by Gen-ik »

I imagine that using substr() would be slightly faster, as long as you know how many characters to need to chop off the string (in this case four) then it's probably better to use it.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

also note that there are fileextensions longer than three characters, e.g. .jpeg .mpeg .java ...
Don't know wether you have to deal with that.
macewan
Forum Commoner
Posts: 97
Joined: Mon Jul 07, 2003 8:27 pm

Post by macewan »

hrm, good point volka. my first idea is to use it only for screenshots or a tutorial page with .txt files. but in the case of scaling it to other files the string replace idea is definately better as Gen-ik pointed out.
will
Forum Contributor
Posts: 120
Joined: Fri Jun 21, 2002 9:38 am
Location: Memphis, TN

Post by will »

if you are dealing with unknown image extensions, you could always go with something like

Code: Select all

$basename = preg_replace('/\.(jpg|jpeg|gif|png)$/i', '', $filename);
tailor as need to work with whatever extensions you want. or you could just have it knock off everything after the last period...

Code: Select all

$basename = preg_replace('/\.[^\.]*$/i', '', $filename);
... just thought i'd throw out a few more options :)
macewan
Forum Commoner
Posts: 97
Joined: Mon Jul 07, 2003 8:27 pm

Post by macewan »

this will be used definately.

Code: Select all

<?php
$basename = preg_replace('/\.[^\.]*$/i', '', $filename); 

?>

thanks
will
Forum Contributor
Posts: 120
Joined: Fri Jun 21, 2002 9:38 am
Location: Memphis, TN

Post by will »

macewan wrote:this will be used definately.

Code: Select all

<?php
$basename = preg_replace('/\.[^\.]*$/i', '', $filename); 

?>

thanks
well, you don't actually need the "i" after the slash. i had copied that from the other one, but it's not needed in this one (it makes the search case insensitive by the way).
macewan
Forum Commoner
Posts: 97
Joined: Mon Jul 07, 2003 8:27 pm

Post by macewan »

k.

once again thanks =)
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Another way to deal with different extension is to explode on the dot. That assumes there are no dots in the file name - not a good idea anyway because of the pathinfo dot bug.
Post Reply