content between editable regions

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

Moderator: General Moderators

Post Reply
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

content between editable regions

Post by Skittlewidth »

I was using a regex to capture all the content of a page between the title tags and the body tags
which was:

Code: Select all

preg_match("/<title>(.*?)<\/title>.*?<body>(.*?)<\/body>/mis",$filetext, $matches);
But then I needed to start being a bit more selective so I added editable regions to the template file but I'm having a bit of trouble with the modified regex:

Code: Select all

preg_match("/<!-- TemplateBeginEditable name=\"title\" -->(.*?)<!-- TemplateEndEditable -->.*?<!-- TemplateBeginEditable name=\"content\" -->(.*?)<!-- TemplateEndEditable -->/mis",$filetext, $matches);
As you can see there are two regions I need to snatch the content from, but at the moment it doesn't get anything. Is it because there are spaces within the template tags? Or should I use more metacharacters and not try to be so literal? :?:
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post by anjanesh »

What happens with this ?

Code: Select all

preg_match("/<.*?TemplateBeginEditable name=\"title\".*?>(.*?)<.*?TemplateEndEditable.*?>.*?<.*?TemplateBeginEditable name=\"content\".*?>(.*?)<.*?TemplateEndEditable.*?>/mis", $filetext, $matches);
User avatar
Skittlewidth
Forum Contributor
Posts: 389
Joined: Wed Nov 06, 2002 9:18 am
Location: Kent, UK

Post by Skittlewidth »

I think I tried something along those lines, but I just got this to work anyway:

Code: Select all

preg_match("/<\W{3} InstanceBeginEditable name=\"title\" \W{3}(.*?)<\W{3} InstanceEndEditable \W{2}>.*?\<\W{3} InstanceBeginEditable name=\"content\" \W{2}>(.*?)<\W{3} InstanceEndEditable \W{2}>/mis", $filetext, $matches)
Thanks for the suggestion though 8)

[edit] I've just realised that I had problems to start with because the editable regions in a template are marked up differently to the way they are marked out in a file created from the template i.e one is a "TemplateBeginEditable" and the other is " Instance BeginEditable" duh no wonder I had problems....[/edit] :oops: [/i]
Post Reply