Jump to content

Preg Replace Help


Dragosvr92

Recommended Posts

So i have this script  that outputs Listed Files inside a folder

 

    echo '<option value="'.$fileName.'">'.$fileName.'</option>';}

 

like this :

 

<option value="Fonts/28 Days Later.ttf">Fonts/28 Days Later.ttf</option> etc files ....

 

i would like to know how may i remove  " Fonts/ " And the .ttf extension from the second part that repeats (the name) using something like preg_replace and to make the first letter uppercase using ucfirst()

 

 

And if someone is familiar with preg_replace

could you please give me a tutorial on how it works to replace some text ?

 

Thanks In Advance

Link to comment
Share on other sites

Hello TheKiller.

 

Is using preg_replace important?

 

Personally I would use str_replace as it is faster than preg_replace and test have shown this, just google "preg_replace VS str_replace"

 

If all of the files are going to have the same directory name and extension you can use the following:

$neatFileName = str_replace('Fonts/','' $fileName);
$neatFileName = str_replace('.ttf','' $neatFileName);

echo '<option value="'.$fileName.'">'.$neatFileName.'</option>';}

 

Or if the extensions and the directory name change use the following function

function neatFileName($filename) {
  $neatFileName = explode("/", $filename);
  $neatFileName = explode(".", $neatFileName[1]);
  return ucfirst($neatFileName[0]);
}

echo '<option value="'.$fileName.'">'.neatFileName($fileName).'</option>';}

 

Let me know how these work out for you, if there any help at all :)

 

Thanks, Paul.

Link to comment
Share on other sites

Hi Paul

nah... its not needed to use preg_replace.. but i thought thats the only way it can be done

 

all files have the same directory...

 

and your first code looks pretty and simple :D

but you missed to add a dot or a comma in it :P

i arranged it like this ..but sadly its not working :(

 

<?php
$fileListDir = "Fonts/*";
$fileList = glob($fileListDir);
$neatFileName = str_replace('Fonts/','' ,$fileName);
$neatFileName = str_replace('.ttf','' ,$neatFileName);

foreach ($fileList as $fileName) {


echo '<option value="'.$fileName.'">'.$neatFileName.'</option>';}
?>

 

 

 

your Second script works as it should :D

 

<?php
$fileListDir = "Fonts/*";
$fileList = glob($fileListDir);

function neatFileName($filename) {
  $neatFileName = explode("/", $filename);
  $neatFileName = explode(".", $neatFileName[1]);
  return ucfirst($neatFileName[0]);
}

foreach ($fileList as $fileName) {
echo '<option value="'.$fileName.'">'.neatFileName($fileName).'</option>';}
?>

 

 

Thank You Very much ! :)

 

but can you figure whats up with the first code ?

it looks more simple than this and its shorter.. which would be faster ?

Link to comment
Share on other sites

The first code won't work as the $neatFieldName variable is being set outside of the foreach statement.

 

I'm not 100% sure which would be faster, but the function is probably better to work with and edit :)

 

Try the following if you want the first piece of code to work

<?php
$fileListDir = "Fonts/*";
$fileList = glob($fileListDir);
foreach ($fileList as $fileName) {
  $optionName = $fileName;
  $neatFileName = str_replace('Fonts/','' ,$fileName);
  $neatFileName = str_replace('.ttf','' ,$neatFileName);
  echo '<option value="'.$optionName.'">'.$neatFileName.'</option>';
}
?>

 

If not then stick with the second piece :)

 

Thanks, Paul.

Link to comment
Share on other sites

err the script seems to work

but it dosnt use the ucfirst.. so i'll stick with the function lol

 

Thank You again :)

Solved

 

edit:

PaulRyan is my hero

lol...

 

Btw.. could you look at this posts i made to see if you can help?

 

http://www.phpfreaks.com/forums/index.php/topic,308562.msg1457982.html#msg1457982

http://www.phpfreaks.com/forums/index.php/topic,308556.msg1458010.html#msg1458010

 

Link to comment
Share on other sites

I noticed the silly mistake I made in the non-function code TheKiller, sorry my bad :)

 

Look here...

<?php
$fileListDir = "Fonts/*";
$fileList = glob($fileListDir);
foreach ($fileList as $fileName) {
  $optionName = $fileName;
  $neatFileName = str_replace('Fonts/','' ,$fileName);
  $neatFileName = ucfirst(str_replace('.ttf','' ,$neatFileName));
  echo '<option value="'.$optionName.'">'.$neatFileName.'</option>';
}
?>

 

Thanks, Paul.

Link to comment
Share on other sites

Ahh yeah that, its for a game I play, just an application that retrieves and displays data :) Currently adding the rest of the features to it.

 

Yeah I am fond of dark coloured layouts and designs, much more pleasing to the eye in my opinion.

 

Sure thing TheKiller, I'll look forward to it when you've finished.

 

Thanks, Paul.

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.