Regex between < >
I have a stri开发者_如何学Cng 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.
$string = "Pretext<thecontentineed>";
preg_match("/<(.*?)>/" , $string, $output);
You forgot the ()
if (preg_match('/<(.*?)>/', $string, $output)) {
echo $output[1];
}
$string = "Pretext<thecontentineed>";
preg_match("/\<([^>]+)\>/" , $string, $output);
print_r($output);
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.
if (preg_match('/Pretext<(.*?)>/', $string, $output)) {
echo $output[1];
}
精彩评论