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?

Link to comment
Share on other sites

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

        )

 

)

Link to comment
Share on other sites

$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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.