Current location: Hot Scripts Forums » Programming Languages » PHP » PHP Regular Expressions


PHP Regular Expressions

Reply
  #1 (permalink)  
Old 11-04-05, 02:19 PM
Jaffizzle Jaffizzle is offline
Newbie Coder
 
Join Date: May 2005
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Regular Expressions

Is it possible to have a regular expression do this?

I want to replace all emails with "some string" except emails that contain the word "hello"

So for example
blahblah@somemail.com changes to "some string"
haha@haha.com changes to "some string"
hello@foo.com stays as hello@foo.com
foo@hello.com stays as foo@hello.com

I cant seem to find out how to ignore strings.

Thanks!!
Reply With Quote
  #2 (permalink)  
Old 11-04-05, 03:14 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 Jaffizzle
Is it possible to have a regular expression do this?

I want to replace all emails with "some string" except emails that contain the word "hello"

So for example
blahblah@somemail.com changes to "some string"
haha@haha.com changes to "some string"
hello@foo.com stays as hello@foo.com
foo@hello.com stays as foo@hello.com

I cant seem to find out how to ignore strings.
Yes, you can do this. Just have PHP look at each email, where it contains "hello", do nothing, otherwise change it. You could also do this with strpos and several other ways as well.
__________________
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
  #3 (permalink)  
Old 11-04-05, 04:25 PM
Jaffizzle Jaffizzle is offline
Newbie Coder
 
Join Date: May 2005
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks for the reply, but I guess I should have been more clear.

I was tried using a simplified example of what im trying to do in the above example.

Heres a more real example.

PHP Code:

ob_start();

include 
"somefile.php";
$buffer ob_get_contents();
ob_end_clean();

//i want to append ?opt=$var to all instances of PHP self where ?opt or &opt doesnt exist so $PHP_SELF will change to $PHP_SELF?opt=$var while $PHP_SELF?opt=polls will still be $PHP_SELF?opt=polls

echo ereg_replace('the regular expression im looking for'$PHP_SELF."?opt=".$var."&"$buffer); 
My knowledge of regular expression is very limited From all the examples i seen, i havent found a way to blacklist strings.

Thanks!!
Reply With Quote
  #4 (permalink)  
Old 11-05-05, 07:02 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
To disavow a match use the "!" modifer:

/(dog)/ --will match "dog"

/!(dog)/ --explicitly does not match "dog"

You'll want to separate matching/non-matching elements with a pipe "|" symbol:

/!(|dog|cat)/ -- Will explicitly not match dog or cat


It's a bit more complicated than that, but that should get you started.


Quick Ref:
=============================================

Modifiers
i case-insensitive pattern matching.
g global replace, or replace all
m Treat string as multiple lines. That is, change ``^'' and ``$'' from matching at only the very start or end of the string to the start or end of any line anywhere within the string
s Treat string as single line. That is, change ``.'' to match any character whatsoever, even a newline, which it normally would not match.
x Extend your pattern's legibility by permitting whitespace and comments.

Special Characters
The following should be escaped if you are trying to match that character

\ ^ . $ | ( ) [ ]
* + ? { } ,

Special Character Definitions
\ Quote the next metacharacter
^ Match the beginning of the line
. Match any character (except newline)
$ Match the end of the line (or before newline at the end)
| Alternation
() Grouping
[] Character class
* Match 0 or more times
+ Match 1 or more times
? Match 1 or 0 times
{n} Match exactly n times
{n,} Match at least n times
{n,m} Match at least n but not more than m times
More Special Character Stuff
\t tab (HT, TAB)
\n newline (LF, NL)
\r return (CR)
\f form feed (FF)
\a alarm (bell) (BEL)
\e escape (think troff) (ESC)
\033 octal char (think of a PDP-11)
\x1B hex char
\c[ control char
\l lowercase next char (think vi)
\u uppercase next char (think vi)
\L lowercase till \E (think vi)
\U uppercase till \E (think vi)
\E end case modification (think vi)
\Q quote (disable) pattern metacharacters till \E

Even More Special Characters
\w Match a "word" character (alphanumeric plus "_")
\W Match a non-word character
\s Match a whitespace character
\S Match a non-whitespace character
\d Match a digit character
\D Match a non-digit character
\b Match a word boundary
\B Match a non-(word boundary)
\A Match only at beginning of string
\Z Match only at end of string, or before newline at the end
\z Match only at end of string
\G Match only where previous m//g left off (works only with /g)
__________________
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
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

Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular expressions problem in php EvilDeveloper PHP 2 07-13-08 06:02 AM
Mastering Regular Expressions in PHP rorycanyon PHP 0 10-24-05 07:27 AM
Regular Expressions perleo JavaScript 2 09-28-05 03:44 PM
Regular Expressions djwayne_2004 PHP 1 09-09-05 12:08 PM
PHP multi-dimensional array sorting issue aqw PHP 2 06-24-05 11:09 PM


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