strip text from a string

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
wallabee
Forum Newbie
Posts: 20
Joined: Mon Apr 21, 2003 10:01 pm

strip text from a string

Post by wallabee »

i have a bunch of files that are basically named "totalDesiredString.txt" where "DesiredString" is a varying name throughout the files. What I'd like to do is take "totalDesiredString.txt", and strip it of it's total and it's .txt, leaving only "DesiredString", which can be used to present relevent text boxes. IE "DesiredString's Number of Pictures" blah blah.

Anyone know of a function that does what I'm doing, or a function that will, through a series of processes, lead me to where I need to be? Thanks, you've all been a great help this far.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Have you looked at the Manual's filesystem functions? fopen and it's associated funcs should help you out. :)
wallabee
Forum Newbie
Posts: 20
Joined: Mon Apr 21, 2003 10:01 pm

Post by wallabee »

i guess i don't really see how this would apply.

we're dealing strictly with strings. if you want to, pretend that "totalBlah.txt" isn't a text file, it's just a string which is "totalBlah.txt". I want to take "totalBlah.txt" and turn it into the string "Blah".

accessing the text files and what's within them I have no problem with.

thanks for the input anyway, though.
User avatar
patrikG
DevNet Master
Posts: 4235
Joined: Thu Aug 15, 2002 5:53 am
Location: Sussex, UK

Post by patrikG »

preg_match("/total(\w).txt/",$myFileName,$matches) should return an array "$matches" which contains any sequence of characters inbetween "total" and ".txt".
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

$string = substr_replace($string, 5, -4);
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

If total and .txt are constant:

Code: Select all

$filename = 'totalDesiredString.txt';
$string = str_replace(array('total', '.txt'), '', $filename);
Mac
Post Reply