Page 1 of 1

splitting variables??

Posted: Fri Feb 07, 2003 8:51 am
by jacquesy
I have a php variable:

$image_name = "image.gif";

which comes out of the database.

I want to display this variable on the page but taking away the .gif at the end to just show the image name.

Is this possible?

Thanks!

Use preg_replace

Posted: Fri Feb 07, 2003 3:59 pm
by Tony121076
Use this line to clip the .gif off the variable...

$image_name = preg_replace("/\..*/", "", $image_name);

Posted: Sun Feb 09, 2003 4:53 am
by jacquesy
ok, this work very nice, thanks!

How would I do this to cut off the image name so that I just get .gif?

Thanks.

splitting

Posted: Mon Feb 10, 2003 10:16 am
by foot123
Hi, i use the 'split' function like this:


$image_name="image.gif";
$split_image=split('.',$image_name);
echo $split_image[0];

You can still use preg_replace

Posted: Mon Feb 10, 2003 10:34 am
by Tony121076
split will work. You can also do this...

$image_name = preg_replace("/*.\./", "", $image_name);

Oops, Sorry, here is correction...

Posted: Mon Feb 10, 2003 10:54 am
by Tony121076
Sorry, that line will produce an error. This is the correct line...

$image_name = preg_replace("/.*.\./", "", $image_name);

Posted: Mon Feb 10, 2003 11:10 am
by jacquesy
these do not seem to work, I get the error message:

Compilation failed: nothing to repeat at offset 0

What does this mean?

thnaks.

Sorry, I don't know why it's not working.

Posted: Mon Feb 10, 2003 1:34 pm
by Tony121076
Sorry, I don't know. The split should work. You can do it like this to turn "myimage.gif" into ".gif"...

$split_image = preg_split(".", $image_name);
$image_name = "." . "$split_image[1]";

Posted: Mon Feb 10, 2003 2:29 pm
by jacquesy
No still not working Tony, it's very odd!