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
Share on other sites

In the end of your pattern you have the closing < /b>. The function thinks that that slash you have in </b> is closing the pattern, so it thinks b is a modifier. The solution- simply escape that slash:

 

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

 

Orio.

Link to comment
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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

^ 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>

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.