Page 1 of 1

Extracting multine string

Posted: Tue Dec 19, 2006 5:40 am
by mzfp2
I have a very simple reg ex function which matches a custom tag as follows :

<#if:var1:eq:var2>

var1 can be any string, and in many instances can be a multi line string, causing the rest of the tag too movie down :

I have created a reg ex function to extract these tags from a file and works perfectly for thosae instance where var1 is NOT multi line :

Code: Select all

preg_match_all("/<\#if:.*?>/s", $Template, $StartTags, PREG_OFFSET_CAPTURE);
This returns the whole tag, but fails when var1 (.*?) spans across multipple lines. Addeding the /s and /m modifiers has no effect!

It does however return everything upto the first newline \n eg, some sample output :

Code: Select all

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => <#if:600mg Glucosamine Sulphate
                    [1] => 1624
                )

        )
)
After the word Sulphate it stops as it has encountered a newline character, is there anyway too make it retreive everything including newline characters? and therefore return the rest of match, without having to manually replace newline characters?

Tried allsorts with this so any help would be great!







Posted: Tue Dec 19, 2006 6:04 am
by Skittlewidth

Code: Select all

preg_match_all("/<\#if:.*?>/sm", $Template, $StartTags, PREG_OFFSET_CAPTURE);
Just wondering how you put in the 'm' modifier when you tried it. You don't need a leading slash, just put it right after your delimiter (which in your case happen to be slashes)

Posted: Tue Dec 19, 2006 6:17 am
by mzfp2
Hi SkittleWidth,

thanks, but i' tried it doesn't work.

I think I have isolated the problem, if a replace all the <br /> tahs with a newline character on template string, before performing a regex on it, it works fine :

Code: Select all

$Template = str_replace("<br />", "\n", $Template);
    preg_match_all("#<\#if:.*?>#s", $Template, $StartTags, PREG_OFFSET_CAPTURE);
</php]

now the only problem is it, I need to work with offsets, as this code is part of a template parsing system, and replacing all <br /> tags with a character affects the offets, therefore in essence the preg_mtach_all would be returning the correct string, but invalid offfsets.

I'm wondering why the /s or m modifier do not function withh lines containing the <br /> tag>