Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

regexp help please!

Status
Not open for further replies.

georgeocrawford

Technical User
Aug 12, 2002
111
0
0
GB
OK.

Here's my pattern so far:

Code:
$pattern = '/ \<  ([[:alnum:]]+)  \> ( (?>[^<>]+) | (?R) )*   \< \/ \1 \> /Xx';

What I want to match is all the text between an opening and closing HTML-like tag. I want to allow for nested tags, so the regexp should match up until where the opening tag is properly closed.

Here is a test string:

Code:
$string = 'text <replace>comment1<fish>comm<replace>noddy asd</replace>ent2</fish>sdsdsds</replace>text2<gordon>doodle<food>asd</food>qw qwqw</gordon>  garn';

I am using the following call:

Code:
$chunks = preg_split($pattern, $string, -1, PREG_SPLIT_DELIM_CAPTURE);

What i need is a regexp which sets $chunks like this:

Code:
Array
(
    [0] => text 
    [1] => replace
    [2] => comment1<fish>comm<replace>noddy asd</replace>ent2</fish>sdsdsds
    [3] => text2
    [4] => gordon
    [5] => doodle<food>asd</food>qw qwqw
    [6] =>   garn
)

I am going nuts here. Please could someone put me on the right lines!

______________________

George
 
This seems to do what I want:

Code:
$pattern = '/ \< ([[:alnum:]]+) \> ( (?>[^<>]+) | (?R) )* \< \/ \1 \>  /Xx';

while(preg_match($pattern, $string, $captured))
{
   $string = preg_replace('/'.addcslashes($captured[0], '/').'/', '', $string, 1);
   $results[][$captured[1]] = preg_replace(array("/ ^\<$captured[1]\> /Xx", "/ \<\/$captured[1]\>$ /Xx"), array(), $captured[0], 1);
}

Are there any suggestions for improvement?

______________________

George
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top