Truncate problem

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
S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

Truncate problem

Post by S_henry »

I have a simple question. Let say I have one string (e.g. 'football') but I just want to display(echo) 'foot'. How can I do that? Any idea?
S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

Truncate problem

Post by S_henry »

Let me explain more detail. Actually in my table, 1 of my data is the file name (eg: abc.txt, def.ppt). In my page, I only want to display 'abc' or 'def' without their extension. So, any idea pls..
User avatar
Michael 01
Forum Commoner
Posts: 87
Joined: Wed Feb 04, 2004 12:26 am

Post by Michael 01 »

There are several methods for this and my recommendations are the following:

ereg_replace

OR

str_replace

Both functions can work the way you wish without creating a function of your own, however if you wish to use upload scripts, creating your own full function extension mask manager may be a good idea as well. :D
S_henry
Forum Contributor
Posts: 148
Joined: Sun Jan 25, 2004 10:25 pm
Location: M'sia

Truncate problem

Post by S_henry »

Thanx..so simple.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You can also use [php_man]basename[/php_man](), by itself if the extension will always be the same or in conjunction with [php_man]pathinfo[/php_man]() if it will be variable. E.g:

Code: Select all

$path_parts = pathinfo($file_name);
$file_name_no_extension = basename($file_name, '.'.$path_parts['extension']);
Mac
User avatar
Michael 01
Forum Commoner
Posts: 87
Joined: Wed Feb 04, 2004 12:26 am

Post by Michael 01 »

I have heard of the pathinfo function, but never the baseline. I love coming here because a person learns something new everyday. :D
Post Reply