TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 07-20-2009, 03:08 AM   #1 (permalink)
The Wanderer
 
superthin's Avatar
 
Join Date: Aug 2008
Posts: 16
Thanks: 7
superthin is on a distinguished road
Default Give me remove special characters from a string, plz

Hello everybody,

I am a newbie. I want to remove all special characters from a string (keep alpha-numeric character only). And replace a space (or many spaces) with "-" character.

Code:
Ex: This is bullshit M$ Word. They make   me 100% crazy.
Will be: This-is-bullshit-M-Word-They-make-me-100-crazy
Any help will be appreciated. Thanks in advance.

Best regards,
SuperThin.
__________________
http://khoancatbetong.com - Vietnamese Concrete Technician Forum
superthin is offline  
Reply With Quote
Old 07-20-2009, 03:19 AM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,298
Thanks: 17
Village Idiot is on a distinguished road
Default

This should do it
PHP Code:
// To strip special chars
preg_replace("[!-@]","",$subject);

//To make all spaces -
str_replace(" ","-",$subject);

//Take any sequince of two or more -s and make them one.
preg_replace("[-]{2,}","-",$subject); 
I wrote this off the top of my head, so I can't guarantee that its perfect.
__________________

Village Idiot is offline  
Reply With Quote
The Following User Says Thank You to Village Idiot For This Useful Post:
superthin (07-20-2009)
Old 07-20-2009, 11:13 AM   #3 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,381
Thanks: 5
Salathe is on a distinguished road
Default

You could also use a single call to preg_replace with multiple patterns/replacements.

PHP Code:
$subject 'This is bullshit M$ Word. They make   me 100% crazy.';
$search  = array('/[^a-z0-9 ]+/i''/ +/');
$replace = array('''-');
echo 
preg_replace($search$replace$subject);
// Output: This-is-bullshit-M-Word-They-make-me-100-crazy 
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
superthin (07-20-2009)
Old 07-29-2009, 12:51 AM   #4 (permalink)
The Wanderer
 
superthin's Avatar
 
Join Date: Aug 2008
Posts: 16
Thanks: 7
superthin is on a distinguished road
Default

Wow, thank you(s) very much. Now, I want to "Do not replace . (point) / Point is not a special character". What should I do? It seems to very hard to grasp Regular Expression for beginners.
__________________
http://khoancatbetong.com - Vietnamese Concrete Technician Forum
superthin is offline  
Reply With Quote
Old 07-29-2009, 10:04 AM   #5 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,381
Thanks: 5
Salathe is on a distinguished road
Default

Just change [^a-z0-9 ] in my previous post to [^a-z0-9. ]. Simples.

As for learning regular expressions, if you haven't already then please bookmark and visit http://www.regular-expressions.info/ as it is the best concise online source of information about regular expressions that I've found so far.
Salathe is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
add special characters to DB? arale Absolute Beginners 0 07-08-2009 04:33 AM
Special to plain characters ReSpawN Advanced PHP Programming 3 05-27-2009 10:33 AM
Selecting special characters in LIKE (mySQL) ReSpawN Advanced PHP Programming 1 04-15-2009 02:27 PM
array elements into variables and values Dave Absolute Beginners 7 06-20-2008 01:56 PM
remove from string... how to do? Sam Granger Absolute Beginners 2 12-04-2007 07:17 AM


All times are GMT. The time now is 10:36 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design