Goldeneye Posted September 2, 2009 Share Posted September 2, 2009 I'm attempting to create a Regular Expression to match text that is following a [/h1] tag. I'm trying to write it in such a way that *if* (meaning whether or not they occur is optional) the number of line-breaks/carriage-returns doesn't matter. <?php preg_match_all('|\[/h1\][\\n]?{1,}(.*?)|si', $information, $matches); ?> I hope this is clear enough. A preemptive thanks. Quote Link to comment Share on other sites More sharing options...
Adam Posted September 2, 2009 Share Posted September 2, 2009 Not being clear enough. You want to match everything after a [h1] tag, or to another certain tag / point? Quote Link to comment Share on other sites More sharing options...
Goldeneye Posted September 2, 2009 Author Share Posted September 2, 2009 Not being clear enough. You want to match everything after a [h1] tag, or to another certain tag / point? Matching text from [/h1] to another [h1] tag. Quote Link to comment Share on other sites More sharing options...
Garethp Posted September 2, 2009 Share Posted September 2, 2009 '~[/h1](.+)[h1]~' doesn't work? Quote Link to comment Share on other sites More sharing options...
Garethp Posted September 2, 2009 Share Posted September 2, 2009 Or how about '~[/h1]\s+([a-zA-Z0-9])\s+[h1]~' Unless there's line breaks inside, then might this work? '~[/h1]\s+(?:\s+([a-zA-Z0-9])\s+)+\s+[h1]~' Quote Link to comment Share on other sites More sharing options...
Adam Posted September 2, 2009 Share Posted September 2, 2009 Try: /[\/h1](.*?)[h1]/s Quote Link to comment Share on other sites More sharing options...
Garethp Posted September 2, 2009 Share Posted September 2, 2009 Try: /[\/h1](.*?)[h1]/s I suggested that, but then I realized that the poster said he just wanted the text, whilst that would capture the new lines Though I suppose you could step outside of the regex mindset and just strip the newlines from the result... or you could use regex to strip the newlines XD Quote Link to comment Share on other sites More sharing options...
Adam Posted September 2, 2009 Share Posted September 2, 2009 Matching text from [/h1] to another [h1] tag. He wants to match all text (regardless of new lines) following a [/h1] to [h1], ignoring that you shouldn't use more than h1 tag, he'd need to add the 's' modifier to the regexp in order to match new lines. Your examples didn't include that. Quote Link to comment Share on other sites More sharing options...
Goldeneye Posted September 2, 2009 Author Share Posted September 2, 2009 Ahhh it's all so obvious now. It does work but there's only slight problem that I should've mentioned earlier - I need to preserve the text inside the [h1] tags. Here's what I'm using right now. <?php preg_match_all('/\[\/h1\](.*?)\[h1\]/si', $information, $matches); ?> Using the $matches[0] index results in: [/h1] Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non feugiat mauris. Nullam et ante a odio cursus blandit eu eget justo. Ut sit amet augue et leo vestibulum rhoncus ut ut ante. [h1] [/h1] Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non feugiat mauris. Nullam et ante a odio cursus blandit eu eget justo. Ut sit amet augue et leo vestibulum rhoncus ut ut ante. [h1] [/h1] Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non feugiat mauris. Nullam et ante a odio cursus blandit eu eget justo. Ut sit amet augue et leo vestibulum rhoncus ut ut ante. [h1] [/h1] Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed non feugiat mauris. Nullam et ante a odio cursus blandit eu eget justo. Ut sit amet augue et leo vestibulum rhoncus ut ut ante. [h1] Edit: I tried this code: <?php preg_match_all('/\[h1\](.*?)\[\/h1\](.*?)/si', $row['content'], $matches); ?> But now it only returns the text in the [h1] [/h1] tags. Quote Link to comment Share on other sites More sharing options...
Goldeneye Posted September 3, 2009 Author Share Posted September 3, 2009 Nevermind, thanks for your input MrAdam and Garethp. I got frustrated with Regular-Expressions and took a different route: <?php $element = explode('[h1]', $information); foreach($element as $section) $paragraphs[] = (!empty($section) && !is_bool(strpos($section, '[/h1]'))) ? '[h1]'.$section : $section; ?> 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.