Current location: Hot Scripts Forums » Programming Languages » PHP » ereg_replace


ereg_replace

Reply
  #1 (permalink)  
Old 10-11-09, 05:27 AM
alane alane is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 71
Thanks: 6
Thanked 0 Times in 0 Posts
ereg_replace

Hi

I am updating a script wrote a script few years ago using the following
PHP Code:

$entry ereg_replace("[^A-Za-z0-9]"""$entry
to remove everything but alpha numatic characters but I understand that the function will no longer exists in PHP6 and that the use now of this function is 'highly discouraged'

I think the correct replacement is preg_replace. Is this correct and does everything within the brackets remain unchanged?

Further quick question in the first part within brackets ("[^A-Za-z0-9]" what does the ^ actually do? I can't for the life of me remember why it's there.

Thanks
Reply With Quote
  #2 (permalink)  
Old 10-11-09, 07:20 AM
captcha captcha is offline
Newbie Coder
 
Join Date: Jul 2009
Posts: 24
Thanks: 0
Thanked 1 Time in 1 Post
preg_replace (in the form of '/[^A-Za-z0-9]/') should work, yes.

The ^ negates everything in the [] brackets. you are matching everything that is NOT (^) a character between A-Z, a-z and 0-9 and replace it with nothing ('')
Reply With Quote
The Following User Says Thank You to captcha For This Useful Post:
alane (10-11-09)
  #3 (permalink)  
Old 10-11-09, 12:50 PM
alane alane is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 71
Thanks: 6
Thanked 0 Times in 0 Posts
Thanks for that.

May I ask, just for the 'learning curve' the purpose of the two / in the function please.
Reply With Quote
  #4 (permalink)  
Old 10-11-09, 01:35 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by alane View Post
May I ask, just for the 'learning curve' the purpose of the two / in the function please.
They're the delimiters that mark the start and stop of the regular expression. You can use any delimiter you want, but slashes and pipes (|) are common. If you use one that's also a meta character you may run into problems when the regex is evaluated.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #5 (permalink)  
Old 10-11-09, 03:42 PM
captcha captcha is offline
Newbie Coder
 
Join Date: Jul 2009
Posts: 24
Thanks: 0
Thanked 1 Time in 1 Post
http://php.net/manual/en/intro.pcre.php is a good place to start reading about delimiters / pattern modifiers / general (PHP) regex handling
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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

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

Forum Jump


All times are GMT -5. The time now is 08:00 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.