Jump to content

Stripping underscores and extensions within an if statement


mr_nabo

Recommended Posts

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]
Link to comment
Share on other sites

[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);
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.