Page 1 of 1
preg_match_all???
Posted: Thu Jun 28, 2007 7:00 pm
by alex.barylski
Edit: This is driving me nutts. If I remove the first '<' everything works as expected...if I escape it nothing...WTF is going here?
Trying to eumulate SSI technology...this is what I have:
Code: Select all
preg_match_all('/<!--#include="(.+)" -->/', $buff, $matches, PREG_PATTERN_ORDER);
Ideally I want to have the function return:
a) The full match
b) The stuff in the (.+)
I need to replace the full match, so having that would make using str_replace alot easier. I also don't want to parse the result so having the filename available is kinda nice
I have tried each flag with no luck...what am I doing wrong or what is wrong with my regex???
These are the result I am getting:
Code: Select all
Array
(
[0] => Array
(
[0] =>
[1] =>
)
[1] => Array
(
[0] => includes/header.tpl
[1] => includes/footer.tpl
)
)
Shouldn't the first array be a full match, according to the docs?
Thanks

Posted: Thu Jun 28, 2007 7:17 pm
by feyd
Regex question...

Regex.

Posted: Thu Jun 28, 2007 7:20 pm
by alex.barylski
feyd wrote:Regex question...

Regex.

Is it? I don't know...I thought it could be a argument option...
Posted: Thu Jun 28, 2007 7:28 pm
by feyd
You're echoing this array in the browser, aren't you? .. Check the source.

Posted: Thu Jun 28, 2007 7:37 pm
by alex.barylski
feyd wrote:You're echoing this array in the browser, aren't you? .. Check the source.

Heh? Not sure I follow...I've been over the source a dozen times...
It's not returning the expected results...it's the leading '<' in the pattern. If I remove it then the array is loaded as expected, however the full match is missing the leading '<' which is easily rectified by just appending it at replacement time. Talk about a hack though, especially cause I'm not sure why it works...
I've escaped the leading '<' but still doesn't work...
Posted: Thu Jun 28, 2007 7:47 pm
by feyd
I'm unable to reproduce your problem.
Code: Select all
[feyd@home]>php -r "$buff = '<!--#include=\"includes/header.tpl\" --> blah blah <!--#include=\"includes/footer.tpl\" -->'; preg_match_all('/<!--#include=\"(.+)\" -->/U', $buff, $matches, PREG_PATTERN_ORDER); pri
nt_r($matches);"
Array
(
[0] => Array
(
[0] => <!--#include="includes/header.tpl" -->
[1] => <!--#include="includes/footer.tpl" -->
)
[1] => Array
(
[0] => includes/header.tpl
[1] => includes/footer.tpl
)
)
So the question now is, what's in $buff?
Posted: Thu Jun 28, 2007 8:35 pm
by alex.barylski
feyd wrote:I'm unable to reproduce your problem.
Code: Select all
[feyd@home]>php -r "$buff = '<!--#include="includes/header.tpl" --> blah blah <!--#include="includes/footer.tpl" -->'; preg_match_all('/<!--#include="(.+)" -->/U', $buff, $matches, PREG_PATTERN_ORDER); pri
nt_r($matches);"
Array
(
[0] => Array
(
[0] => <!--#include="includes/header.tpl" -->
[1] => <!--#include="includes/footer.tpl" -->
)
[1] => Array
(
[0] => includes/header.tpl
[1] => includes/footer.tpl
)
)
So the question now is, what's in $buff?
WTF...
$buff is just a plain jane HTML template, composed of SSI style includes like those I am trying to match :S
Posted: Thu Jun 28, 2007 9:34 pm
by John Cartwright
post the contents of a file your using
Posted: Thu Jun 28, 2007 11:43 pm
by alex.barylski
Good idea.
Code: Select all
<!--#include="includes/header.tpl" -->
<div style="background-color: red; width: 100%; height: 200px">
This is a question? Here is a mission statement...
<ul>
<li>Point number 1</li>
<li>Point number 2</li>
<li>Point number 3</li>
</ul>
These are just a few reasons to choose Company XYZ as your solutions provider. Click here to read more.
</div>
Page body goes here
<!--#include="includes/footer.tpl" -->
That it literally it...almost verbatim, minus some marketing verbage replaced with filler text...no funny chars in the verbage it's essentially identical to that I submitted
Posted: Fri Jun 29, 2007 12:02 am
by John Cartwright
I cannot reproduce the error either, php5.1.2
Code: Select all
$buff = '<!--#include="includes/header.tpl" -->
<div style="background-color: red; width: 100%; height: 200px">
This is a question? Here is a mission statement...
<ul>
<li>Point number 1</li>
<li>Point number 2</li>
<li>Point number 3</li>
</ul>
These are just a few reasons to choose Company XYZ as your solutions provider. Click here to read more.
</div>
Page body goes here
<!--#include="includes/footer.tpl" --> ';
preg_match_all('/<!--#include=\"(.+)\" -->/U', $buff, $matches, PREG_PATTERN_ORDER);
echo '<pre>';
print_r($matches);
Code: Select all
Array
(
[0] => Array
(
[0] => <!--#include="includes/header.tpl" -->
[1] => <!--#include="includes/footer.tpl" -->
)
[1] => Array
(
[0] => includes/header.tpl
[1] => includes/footer.tpl
)
)
Posted: Fri Jun 29, 2007 2:32 am
by alex.barylski
I'm running 5.2.1
WTF...
Posted: Fri Jun 29, 2007 2:45 am
by John Cartwright
Posted: Fri Jun 29, 2007 4:34 am
by Jenk
Why don't you post exactly what you are working with? Posting "example" stuff, which works, but then still having problems indicates your actual data is the problem.