Jason Small
Jason Small

Reputation: 1054

Regex between < >

I have a string that looks like:

Pretext<thecontentineed>

I' trying to write a regex that will pull "thecontentineed" from that string using preg_match

I've tried:

$string = "Pretext<thecontentineed>";    
preg_match("/<.*?>/" , $string, $output);

But that returns an empty array.

Upvotes: 1

Views: 821

Answers (5)

Marc B
Marc B

Reputation: 360702

You haven't specified any capturing groups with ():

preg_match('/<(.*)?>/', $string, $matches);

The () instruct the regex pattern to 'capture' whatever matches within the brackets, and store them into the $matches array.

Upvotes: 1

citizen conn
citizen conn

Reputation: 15390

if (preg_match('/Pretext<(.*?)>/', $string, $output)) {
    echo $output[1];
}

Upvotes: 0

Nin
Nin

Reputation: 3020

$string = "Pretext<thecontentineed>";    
preg_match("/<(.*?)>/" , $string, $output);

You forgot the ()

Upvotes: 7

Andreas Wong
Andreas Wong

Reputation: 60536

$string = "Pretext<thecontentineed>";
preg_match("/\<([^>]+)\>/" , $string, $output);
print_r($output);

Upvotes: 3

rid
rid

Reputation: 63472

if (preg_match('/<(.*?)>/', $string, $output)) {
    echo $output[1];
}

Upvotes: 3

Related Questions