Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Old 10-21-2010, 10:39 PM preg replace problem
Skilled Talker

Posts: 83
Trades: 0
I'm having an issue with users uploading files with a Semi Colon in the file name.
I am currently using a script that replaces characters within the file name, however, when I try to add a ";" as invalid characters to replace, it is like the semi-colon just stops the script, as the file does not upload (no errors are displayed).

Here's the code I'm working with:

PHP Code:
$valid_chars_regex '.A-Z0-9_ !@#$%^&()+={}\[\],~`-';    
$file_name preg_replace('/[^'.$valid_chars_regex.']|\'.+$/i'""basename($_FILES[$upload_name]['name']));
    if (
strlen($file_name) == || strlen($file_name) > $MAX_FILENAME_LENGTH) {
        
HandleError("Invalid file name");
        exit(
0);
    } 
I tried placing the semi colon all over the place in the preg_replace function, but nothing seems to work.

How can I do this?
Smudly is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 10-22-2010, 12:33 AM Re: preg replace problem
vivekar's Avatar
Webmaster Talker

Posts: 612
Trades: 0
Have you tried escaping ';' with '\'?

Also, you can try preg_quote.

I have one nifty function which will replace all the special chars. See whether that helps you or not.
PHP Code:
function makeslug($str,$separator='-')
{
    
$str strtolower(trim($str));
    
$str preg_replace('/[^a-z0-9-]/'$separator$str);
    
$str str_replace(' ',$separator$str); // replace spaces by $separator
    
$str preg_replace('/-+/'$separator$str); //replace multiple dashes by underscore
    
$str preg_replace('/_+/'$separator$str); //replace multiple underscore by single underscore
    
$str trim($str,$separator); //remove trailing underscores, if any
    
return $str;

__________________

Please login or register to view this content. Registration is FREE
(Active since 2003) |
Please login or register to view this content. Registration is FREE

Last edited by vivekar; 10-22-2010 at 12:40 AM.. Reason: added php function
vivekar is offline
Reply With Quote
View Public Profile Visit vivekar's homepage!
 
Reply     « Reply to preg replace problem
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.14822 seconds with 12 queries