Help this Neophyte learn REGEX

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Help this Neophyte learn REGEX

Post by neophyte »

I'm finally getting around to tackling REGEX.... Of all the things I've learned about PHP, REGEX was never on the top of my list. Anyway -- I got this little snippet of code to work fine -- basically, I wanted it to remove the whole string including the comment marks. Then I added the extra characters $%#^ etc. and can't get it to work. Does somebody know how I would change this REGEX to replace the entire $String?

Code: Select all

$string = "<!--THIS TEXT I WA#$%^@NT OUT OF HERE-->JHGFJTHFHT";
$clean_string = preg_replace("/<!--&#1111;a-z0-9]\s+-->/i", "", $string);
echo $clean_string;
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

pattern:

Code: Select all

#<!--.*?--\s*>#
edit: fixed to HTML 4.01 W3C compliant.
Last edited by feyd on Sat Jan 22, 2005 9:18 pm, edited 1 time in total.
Cronikeys
Forum Commoner
Posts: 35
Joined: Sun Jan 16, 2005 9:14 am

Post by Cronikeys »

I am just curious how you knew that? I mean how is that determined... perhaps point me in the direction of a tutorial?
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Post by neophyte »

Feyd is the King of REGEX. I've seen him come up with REGEX for things I never though possible. It makes me wonder if he's using REGEX coach or something like that....

Tutorials? try http://www.hudzilla.org

Great place to learn PHP. Including REGEX of course! :D
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I don't know of any great tutorials on regular expression, as they are fairly complex to begin with. I can recommend getting the regex bible: Mastering Regular Expressions (O'Reilly Associates)

I learned regex mostly through experimenation, but since getting the bible, I grew quite a bit in my understanding and limitations on more advanced bits like lookbacks..
Post Reply