Page 1 of 1

chopping from a string

Posted: Thu Aug 21, 2003 5:36 pm
by macewan
don't laugh :?

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

12345678

into

1234

Posted: Thu Aug 21, 2003 5:46 pm
by Gen-ik
$chopped = substr($the_string, 0, -4);

Posted: Thu Aug 21, 2003 5:46 pm
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.

Posted: Thu Aug 21, 2003 6:47 pm
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>

Posted: Thu Aug 21, 2003 6:56 pm
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);
?>

Posted: Thu Aug 21, 2003 7:01 pm
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.

Posted: Thu Aug 21, 2003 7:40 pm
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); 

?>

?

Posted: Fri Aug 22, 2003 7:29 am
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.

Posted: Fri Aug 22, 2003 7:35 am
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.

Posted: Fri Aug 22, 2003 8:07 am
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.

Posted: Fri Aug 22, 2003 9:09 am
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 :)

Posted: Fri Aug 22, 2003 9:52 am
by macewan
this will be used definately.

Code: Select all

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

?>

thanks

Posted: Fri Aug 22, 2003 9:58 am
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).

Posted: Fri Aug 22, 2003 10:28 am
by macewan
k.

once again thanks =)

Posted: Fri Aug 22, 2003 12:26 pm
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.