mr_nabo Posted November 23, 2006 Share Posted November 23, 2006 Hi,I've looked pretty hard to find more information about IF statements and how I could strip underscores/extensions etc. but I'm having trouble getting anywhere.I'm pretty new to PHP but eager to learn. Here's the code I have below, I'd like to strip underscores and extensions like .mpg from the filename, capitalize the first letter of each word and remove the first three characters of the filename.I'm pretty sure the functions I need are str_replace(), trim() and ucwords() but I can't work out how to get them into the code properly.Can anyone help me out? Here's the code.Cheers[code]<!-- display folder contents --><?php function directory($result) { $handle=@opendir("demos"); // while ( conditional statement is true){.....do this code ;} while ($file = readdir($handle)) { // The || means 'or' and '=='' means 'is equal to' so this statement // means that IF the file has one (.) OR two (..) characters to do nothing if ($file == "." || $file == "..") { } // Otherwise... else { // print out directory link and filesize. print "<p class=\"demos\"> <a href=\"demos/$file\">$file</a><br /> (<font class=\"filesize\"><i>size: " . round(filesize("demos/$file")/1024/1024, 2) . " mb / " . round(filesize("demos/$file")/1024, 2) . " kb)</i></font></p>"; } } closedir($handle); return $result; } echo directory($result); ?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/28271-stripping-underscores-and-extensions-within-an-if-statement/ Share on other sites More sharing options...
Hypnos Posted November 27, 2006 Share Posted November 27, 2006 [quote author=mr_nabo link=topic=116083.msg472764#msg472764 date=1164316428]I'd like to strip underscores[/quote]$string = str_replace("_", "", $string);[quote]and extensions like .mpg from the filename[/quote]There are multiple ways to do this. Using regular expressions would probably be more effective (which I'm not very good with).Quick and dirty way to do it (that would break on anything with more than one period):$stringarr = explode(".", $string);$string = $stringarr[0]; Or if you just want to strip certain extensions:$extensions = array(".rar", ".exe", ".txt");$string = str_replace($extensions, "", $string);[quote]capitalize the first letter [/quote]string = ucwords($string);[quote]remove the first three characters of the filename[/quote]$string = substr($string, 3); Quote Link to comment https://forums.phpfreaks.com/topic/28271-stripping-underscores-and-extensions-within-an-if-statement/#findComment-130798 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.