Jump to content

[SOLVED] Matching text that is following a [/h1] tag


Recommended Posts

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.

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

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.

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.

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;
?>

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.