Jump to content

Archived

This topic is now archived and is closed to further replies.

play_

Getting everything between the first > and the last <

Recommended Posts

Can it be done?

I plan on using preg_match_all with this.

now, i know that preg_match_all("\<.b>(.*)<\/b>/", $a, $e) will match everything between <.b> and  </b> and insert it into the array.
I only need to get everything between the first >  and last < on a line, considering there will be more of them in between.

Share this post


Link to post
Share on other sites
actually, i think you've got it slightly backwards. if you want EVERYTHING between the first and last characters you put in, you need to use the (.*)... remember that the '.' is greedy. here's the difference:

[code]
<?php
$text = "<b>asdf</b><i>asdf</i><p></p>";

// first, get only what's between the bold tags:
preg_match('|<b>(.+?)</b>|i', $text, $match);
echo $match[1]; // outputs "asdf"

// now, here's getting everything between first ">" and last "<"
preg_match('|>(.*)<|', $text, $match);
echo htmlentities($match[1]); // outputs "asdf</b><i>asdf</i><p>"
?>
[/code]

hope this helps

Share this post


Link to post
Share on other sites
Thanks Obsidian.
I can't get it to work on my code though.

Here's a sample line:
[code]&lt;</font><font color="#0000bb">f</font><font color="#007700">&gt;<br>&lt;</font><font color="#0000bb">f</font><font color="#007700">&gt;</font><font color="#ff8000">// news query | display the news&gt;[/code]

The < and > actually got replaced by &lt; and &gt; (its from a highlight_string() function).
So if i used
[code]preg_match('|&gt;l(.*)|&lt;', $text, $match);[/code]
should do it right? because i can't get it to

Share this post


Link to post
Share on other sites
[quote author=play_ link=topic=100467.msg396751#msg396751 date=1152838677]
Thanks Obsidian.
I can't get it to work on my code though.

Here's a sample line:
[code]&lt;</font><font color="#0000bb">f</font><font color="#007700">&gt;<br>&lt;</font><font color="#0000bb">f</font><font color="#007700">&gt;</font><font color="#ff8000">// news query | display the news&gt;[/code]

The < and > actually got replaced by &lt; and &gt; (its from a highlight_string() function).
So if i used
[code]preg_match('|&gt;l(.*)|&lt;', $text, $match);[/code]
should do it right? because i can't get it to

[/quote]

you've got your closing pipe in the wrong place:
[code]
<?php
preg_match('|\&lt;(.*)\&gt;|', $text, $match);
?>
[/code]

Share this post


Link to post
Share on other sites

×

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.