0

Possible Duplicate:
get everthing between <tag> and </tag> with php

I'm trying to grab a string within a string using regex. I've looked and looked but I can't seem to get any of the examples I have to work.

I need to grab the html tags <code> and </code> and everything in between them.

Then I need to pull the matched string from the parent string, do operations on both,

then put the matched string back into the parent string.

Here's my code:

$content = "Lorem ipsum dolor sit amet, consectetur adipiscing" .
           " elit.<code>Donec sed erat vel diam ultricies commodo." .
           " Nunc venenatis tellus eu quam suscipit quis fermentum" .
           " dolor vehicula.</code> Etiam bibendum semper nibh, in" .
           " auctor turpis venenatis quis.";

$regex='';

$code = preg_match($regex, $text, $matches);

I've alread tried these without success:

$regex = "/<code\s*(.*)\>(.*)<\/code>/";
$rgex = "/<code>(.*)<\/code>/";
Community
  • 1
  • 1
Nate
  • 579
  • 1
  • 7
  • 14
  • does it have to be a regexp? What language? – Peter Svensson Feb 12 '12 at 21:34
  • 1
    Sorry obligatory link: http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454 – Sam Greenhalgh Feb 12 '12 at 22:01
  • 1
    @PeterLiljenberg Aye, saw your comment in the other one, so I flagged this as the dupe :) It was posted before, but the other one has an answer on it (the fact that it's my answer is like, totally irrelevant xD) – Joe Feb 12 '12 at 22:22

0 Answers0