Jump to content

[SOLVED] Error: unknown modifier


soycharliente

Recommended Posts

I am trying to grab the text inside the tags formatted like this:

<p><b>1. Some stuff being said.</b>

 

It always has the <p> tag, then the <b> tag, then a number (one or two digits), then the text, and finally the closing <b> tag.

 

Warning: preg_match() [function.preg-match]: Unknown modifier 'b'

 

<?php
$pattern = "/^<p><b>\d[.]( )?(\d\w\s)+</b>$/";
?>

 

By the way, I have no idea what I'm doing. I tried to read about it and that pattern came out. I want what's inside the <b> tags. I'm not quite sure how to return that either. Any help?

 

 

Link to comment
https://forums.phpfreaks.com/topic/106345-solved-error-unknown-modifier/
Share on other sites

What are you trying to achieve with ( )? -- an optional space?

Yes.

 

<?php
$pattern = "/^<p><b>\d[.]( )?(\d\w\s)+<\/b>$/";
preg_match($pattern, $text, $matches);
print_r($matches);
?>

 

I am getting back an empty array. I thought that all of the matches would be stored in the third parameter. I guess I'm not getting anything. I should get back 75 matches. There is a list of 75 things I'm trying to parse out of a list from HTML source code.

<p><b>1. Give advice that matters in one sentence. </b> I got run ...</p>
<p><b>2. Tell if someone is lying. </b> Everyone has his theory....</p>
<p><b>3. Take a photo. </b> Fill the frame.....</p>
.
.
.
<p><b>72. Stock an emergency bag for the car.</b></p><p>Blanket. Heavy flashlight. Hand ...</p>
<p><b>73. Caress a woman's neck.</b> Back of your fingers, in a slow fan.</p>
<p><b>74. Know some birds.</b> If you can't pay attention to a bird, then...</p>
<p><b>75. Negotiate a better price.</b></p>

 

I just want the list of stuff. There's so much more code in between all of them.

 

I am trying to grab the text inside the tags formatted like this:

<p><b>1. Some stuff being said.</b>

 

It always has the <p> tag, then the <b> tag, then a number (one or two digits), then the text, and finally the closing <b> tag.

^ needs the accompanying m modifier to turn on multiline mode.

$ cannot be used because the ending b tag is not at the end of the line.

.+? is used as a more general way to scoop out the data.

 

<pre>
<?php
$data = <<<DATA
<p><b>1. Give advice that matters in one sentence. </b> I got run ...</p>
<p><b>2. Tell if someone is lying. </b> Everyone has his theory....</p>
<p><b>3. Take a photo. </b> Fill the frame.....</p>
.
.
.
<p><b>72. Stock an emergency bag for the car.</b></p><p>Blanket. Heavy flashlight. Hand ...</p>
<p><b>73. Caress a woman's neck.</b> Back of your fingers, in a slow fan.</p>
<p><b>74. Know some birds.</b> If you can't pay attention to a bird, then...</p>
<p><b>75. Negotiate a better price.</b></p>
DATA;

preg_match_all('%^<p><b>\d+\.\s*(.+?)</b>%m', $data, $matches);
print_r($matches);
?>
</pre>

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.