I am trying to figure out how to capture one statement if the other one doesn't exist using preg_match.
Sample Text:
<!-- InstanceBeginEditable name="doctitle" -->
<title>BU Libraries | Research Guides | Citing Your Sources</title>
<!-- InstanceEndEditable -->
<div id="standardpgt"><h1><!-- InstanceBeginEditable name="pagetitle" --><strong>Citing Your Sources</strong><!-- InstanceEndEditable --></h1></div>
Because pagetitle exists I want to pull it instead of the doctitle tag. Of course there is tons of other characters in between them, but I wanted to show you a small sample.
If pagetitle didn't exist I would want to grab the contents of doctitle.
The twist is that I'm not using the php code directly, I'm passing in a regex statement through a config file, then a script is taking it and pulling out the 1st group from the statement.
This is what I came up with:
((?!.*?<!--\s*?InstanceBeginEditable\s*?name=\x22pagetitle\x22\s*?-->.*?<!--\s*?InstanceEndEditable\s*?-->)<!--\s*?InstanceBeginEditable\s*?name=\x22doctitle\x22\s*?-->\s*?<title>(.*?)<\/title>\s*?<!--\s*?InstanceEndEditable\s*?-->|<!-- InstanceBeginEditable\s*?name=\x22pagetitle\x22\s*?-->(.*?)<!--\s*?InstanceEndEditable\s*?-->)
What the issue is for some reason php always reads the first empty group as group 1 if it didn't work.
For example in the sample text above it would return
0 -> <!-- InstanceBeginEditable name="pagetitle" --><strong>Citing Your Sources</strong><!-- InstanceEndEditable -->
1 ->
2 -> <strong>Citing Your Sources</strong>
I can't for the life of figure out how to make this work. I also wrote this regex:
(?(?=.*?<!--\s*?InstanceBeginEditable\s*?name=\x22pagetitle\x22\s*?-->.*?<!--\s*?InstanceEndEditable\s*?-->).*?<!-- InstanceBeginEditable\s*?name=\x22pagetitle\x22\s*?-->(.*?)<!--\s*?InstanceEndEditable\s*?-->|.*?<!--\s*?InstanceBeginEditable\s*?name=\x22doctitle\x22\s*?-->\s*?<title>(.*?)<\/title>\s*?<!--\s*?InstanceEndEditable\s*?-->)
But that didn't work either. Thank you very much for the help.
Chris