Jump to content

Getting everything between the first > and the last <


play_

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