PDA

View Full Version : string length


dispector
08-09-2005, 04:34 AM
alright i have a variable called $image

now i use that in an image tag to display an image, then under that i print the same variable for a little caption

ex.
print "<img src='http://www.domain.com/os/$currentFile' width='150' border='1'>";
print "<br>";
print "Name: ";
print "$currentFile";
print "<br><br>";

WHICH DISPLAYS


|--------------|
|--------------|
|----hello.jpg--|
|--------------|
|--------------|
hello.jpg

now my question is, how can i remove the .jpg from the caption area?

e-Raser
08-09-2005, 11:07 AM
Okay, I don't know if this would work but you could use str_replace.
Like,

print "<img src='http://www.domain.com/os/$currentFile' width='150' border='1'>";
print "<br>";
print "Name: ";
$currentfile = str_replace(".jpg","",$currentfile);
$currentfile = str_replace(".jpeg","",$currentfile);
$currentfile = str_replace(".gif","",$currentfile);
$currentfile = str_replace(".png","",$currentfile);
// and so on...
print "$currentFile";
print "<br><br>";

I'm sure there's an easier way though.

Fou-Lu
08-09-2005, 03:26 PM
Really bad code, but it will work:

$currentfile = substr($currentfile, 0, strlen($currentfile)-strlen(end(explode('.', $currentfile)))-1);

This will remove the last occurance of .XXX. So, if you have image.jpg, it will return image. If you have image.user.jepg, it will return image.user and so forth.
Hope that helps!