regex quickie

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

regex quickie

Post by alex.barylski »

Code: Select all

$html = preg_replace("/<!-- START_NAV_MENU -->(.+)<!-- END_NAV_MENU -->/", $menu_html, $html);
1) Will this find a single occurance of the HTML comments and everything inbetween it?
2) When it replaces the comments stay put or do I have to wrap $menu_html with tags again?

Cheers :)
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Have you tried?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Yes...nothing happened...

Latest:

Code: Select all

$html = preg_replace("/\<\!\-\- START_NAV_MENU \-\-\>(.*)\<\!\-\- END_NAV_MENU \-\-\>/", $menu_html, $html);
This appears to be match other comments and reproducing/replicating those a few times??? :?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

".*?"

"s" modifier too. No need for all the escaping, they aren't metacharacters.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

feyd wrote:".*?"

"s" modifier too. No need for all the escaping, they aren't metacharacters.
I didn't think so...but I had no other idea WTF was going on and figured they didn't hurt :P but i'll remove them for sure thanks :)
Post Reply