splitting variables??

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

jacquesy
New php-forum User
New php-forum User
Posts: 12
Joined: Tue Jan 21, 2003 2:27 pm

splitting variables??

Postby jacquesy » Fri Feb 07, 2003 8:51 am

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!

Tony121076
New php-forum User
New php-forum User
Posts: 28
Joined: Fri Jan 03, 2003 4:00 pm

Use preg_replace

Postby Tony121076 » Fri Feb 07, 2003 3:59 pm

Use this line to clip the .gif off the variable...

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

jacquesy
New php-forum User
New php-forum User
Posts: 12
Joined: Tue Jan 21, 2003 2:27 pm

Postby jacquesy » Sun Feb 09, 2003 4:53 am

ok, this work very nice, thanks!

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

Thanks.

foot123
New php-forum User
New php-forum User
Posts: 14
Joined: Wed Nov 06, 2002 4:39 pm

splitting

Postby foot123 » Mon Feb 10, 2003 10:16 am

Hi, i use the 'split' function like this:


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

Tony121076
New php-forum User
New php-forum User
Posts: 28
Joined: Fri Jan 03, 2003 4:00 pm

You can still use preg_replace

Postby Tony121076 » Mon Feb 10, 2003 10:34 am

split will work. You can also do this...

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

Tony121076
New php-forum User
New php-forum User
Posts: 28
Joined: Fri Jan 03, 2003 4:00 pm

Oops, Sorry, here is correction...

Postby Tony121076 » Mon Feb 10, 2003 10:54 am

Sorry, that line will produce an error. This is the correct line...

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

jacquesy
New php-forum User
New php-forum User
Posts: 12
Joined: Tue Jan 21, 2003 2:27 pm

Postby jacquesy » Mon Feb 10, 2003 11:10 am

these do not seem to work, I get the error message:

Compilation failed: nothing to repeat at offset 0

What does this mean?

thnaks.

Tony121076
New php-forum User
New php-forum User
Posts: 28
Joined: Fri Jan 03, 2003 4:00 pm

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

Postby Tony121076 » Mon Feb 10, 2003 1:34 pm

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]";

jacquesy
New php-forum User
New php-forum User
Posts: 12
Joined: Tue Jan 21, 2003 2:27 pm

Postby jacquesy » Mon Feb 10, 2003 2:29 pm

No still not working Tony, it's very odd!


Return to “PHP coding => General”

Who is online

Users browsing this forum: Bing [Bot] and 1 guest