Jump to content


Photo

Getting everything between the first > and the last <


  • Please log in to reply
4 replies to this topic

#1 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 13 July 2006 - 05:02 PM

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.
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 13 July 2006 - 05:23 PM

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:

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

hope this helps
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#3 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 14 July 2006 - 12:57 AM

Thanks Obsidian.
I can't get it to work on my code though.

Here's a sample line:
&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;

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

regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin

#4 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 14 July 2006 - 01:16 PM

Thanks Obsidian.
I can't get it to work on my code though.

Here's a sample line:

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

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


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

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#5 play_

play_
  • Staff Alumni
  • Advanced Member
  • 717 posts

Posted 14 July 2006 - 02:08 PM

ahhh. thanks a bunch Obsidian
regex.kat.sh --- regex library
u.kat.sh ---- url shortener
tabbit.org ---- tabbed pastebin




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users