soycharliente Posted May 19, 2008 Share Posted May 19, 2008 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? Quote Link to comment Share on other sites More sharing options...
Orio Posted May 19, 2008 Share Posted May 19, 2008 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. Quote Link to comment Share on other sites More sharing options...
effigy Posted May 19, 2008 Share Posted May 19, 2008 Or change the delimiter: %^<p><b>\d\.( )?([\w\s]+)</b>$% What are you trying to achieve with ( )?--an optional space? Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 19, 2008 Author Share Posted May 19, 2008 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. Quote Link to comment Share on other sites More sharing options...
effigy Posted May 19, 2008 Share Posted May 19, 2008 Please post a snippet of the data and any other specifications. Quote Link to comment Share on other sites More sharing options...
soycharliente Posted May 19, 2008 Author Share Posted May 19, 2008 <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. Quote Link to comment Share on other sites More sharing options...
effigy Posted May 19, 2008 Share Posted May 19, 2008 ^ 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> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.