preg_match_all???

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

preg_match_all???

Post 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 :)
Last edited by alex.barylski on Thu Jun 28, 2007 7:21 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Regex question... :arrow: Regex. :?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

feyd wrote:Regex question... :arrow: Regex. :?
Is it? I don't know...I thought it could be a argument option...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You're echoing this array in the browser, aren't you? .. Check the source. ;)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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...
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

post the contents of a file your using
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

Good idea. :P

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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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
        )

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

Post by alex.barylski »

I'm running 5.2.1 :(

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

Post by John Cartwright »

For giggles, does

Code: Select all

#include="([^"]+)/im
work?
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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.
Post Reply