Jump to content

matching many patterns that are surrounded by a pattern?


dsaba

Recommended Posts

	$str = '<head>
	<b>one</b>
	<b>two</b>
	<b>three</b>
	<b>four</b>
	<b>five</b>
	</head>
	<b>six</b>
	<bseven</b>';
	$pat = '~<head>.*(<b>(.*?)</b>).*</head>~';
	$lalaz = preg_match_all($pat, $str, $out);
	print_r($out);

 

it outputs:

Array

(

    [0] => Array

        (

        )

 

    [1] => Array

        (

        )

 

)

 

 

 

I want to match everything inside the <b> bold tags, but only if these bold tags occur within the <head</head> tags.. 

What am I doing wrong?

This is the result I get. I want to grab all of them only grabs the last one 'five' and not the rest, how can I make it do that?

 

Array

(

    [0] => Array

        (

            [0] => <head>

<b>one</b>

<b>two</b>

<b>three</b>

<b>four</b>

<b>five</b>

</head>

        )

 

    [1] => Array

        (

            [0] => <b>five</b>

        )

 

    [2] => Array

        (

            [0] => five

        )

 

)

$pat = '~<head>.*(<b>(.*?)</b>).*</head>~s';

 

I want to say:

look in between <head> </head>

 

match all patterns beginning with <b> and followed by any character and any amount of any characters followed by </b>

 

from the regex tutorial here on phpFreaks I am confused by some of the metacharacters:

 

when I say "~<head>.*

i take it to mean start with <head> and must have only 1 of these yet * means 0 or more of these.. I don't see why both of these are together like this

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.