Rafal Zdziech
Rafal Zdziech

Reputation: 29

regex getting number from tags

I am trying get this number which is between tags << >>

333333

from

string = "my subject <<my-support-id=333333>>"

Can you give any advise as I am not getting anywhere

I am trying to get this using preg_match function in php.

Upvotes: 0

Views: 57

Answers (3)

Jay Blanchard
Jay Blanchard

Reputation: 34416

You could do this -

$string = "my subject <<my-support-id=333333>>";
$pattern = '/\d+/';
preg_match($pattern, $string, $matches);
print_r($matches);

And the output would be Array ( [0] => 333333 )

EXAMPLE

Upvotes: 0

nu11p01n73R
nu11p01n73R

Reputation: 26667

How about the regex

/<<[^\d]+(\d+)>>/
  • [^\d]+ Negated character class. would match anything other than digits, \d

  • (\d+) Capture group 1 will contain the digits within the << >>

Regex Demo

Test

preg_match("/<<[^\d]+(\d+)>>/", "my subject <<my-support-id=333333>>",$matches);
print_r($matches[1]);
=> 333333

OR

/<<[^=]+=(\d+)>>/

Test

preg_match ( "/<<[^=]+=(\d+)>>/", "my subject <<my-support-id=333333>>",$matches);
print_r($matches[1]);
=> 333333

Upvotes: 2

violator667
violator667

Reputation: 499

Try this:

preg_match_all('/\=([0-9]+)>/','my subject <<my-support-id=333333>>', $out, PREG_PATTERN_ORDER);
echo $out[1][0];

Upvotes: 1

Related Questions