Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ereg_replace question

Status
Not open for further replies.

proggybilly

Programmer
Apr 30, 2008
110
US
I am using ereg_replace to remove spaces from file names using this line of code:
Code:
$target = ereg_replace(" ","","/tmp/".$_FILES['upload1']['name']);

I also want to remove underscores and hyphens from filenames such as My_file-20100406.doc

I am unfamiliar with using ereg_replace beyond a single option, could someone please point me correctly towards removing spaces, underscores and hyphens please?
 
I think you need something like this

Code:
$removeThese = array("/ /", "/-/", "/_/");
$replacements = array("","","");
$target = ereg_replace($removeThese,$replacements,"/tmp/".$_FILES['upload1']['name']);

-----------------------------------------
I cannot be bought. Find leasing information at
 
Try using preg_replace instead
Code:
$removeThese = array('/ /', '/-/', '/_/');
$replacements = array('','','');
$target = preg_replace($removeThese,$replacements,"This is some test-to_be changin");
echo $target;

-----------------------------------------
I cannot be bought. Find leasing information at
 
Unless you require a regular epxressin replacement, you should avoid using preg_replace, as per the online manual's suggestion:

[URL unfurl="true" said:
http://php.net/manual/en/function.str-replace.php[/URL]]

str_replace

(PHP 4, PHP 5)

str_replace — Replace all occurrences of the search string with the replacement string
Description
mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

This function returns a string or an array with all occurrences of search in subject replaced with the given replace value.

[red]If you don't need fancy replacing rules (like regular expressions), you should always use this function instead of ereg_replace() or preg_replace().[/red]


With that in mind try this:

Code:
$remove=array("_","-"," ");
$with="";

$mystring=str_replace($remove,$with,$filename);

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Hi

Personally I use regular expressions abusively.
Code:
[navy]$target[/navy] [teal]=[/teal] [COLOR=darkgoldenrod]preg_replace[/color][teal]([/teal][green][i]"/[ _-]/"[/i][/green][teal],[/teal][green][i]""[/i][/green][teal],[/teal][green][i]"/tmp/"[/i][/green][teal].[/teal][navy]$_FILES[/navy][teal][[/teal][green][i]'upload1'[/i][/green][teal]][[/teal][green][i]'name'[/i][/green][teal]]);[/teal]

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top