PHP Regular Expression - Get number from string
I have a string sequence that goes like this: <x:object*10/>
.
I want to replace that tag with something else that depends on what number it is.
I match the tag above with /<x:object\*[0-9]+\/>/
and using preg_replace i can r开发者_运维技巧eplace it all with the content I want. I just need that number now.
How can I get the number please?
By capturing it:
/<x:object\*(?P<my_number>[0-9]+)\/>/
It will be captured with name "my_number"...
Full code would be sth like this:
<?php
preg_match_all(`/<x:object\*(?P<my_number>[0-9]+)\/>/`, $where_to_search, $matches);
var_dump($matches); // Dump the matches to see in detail.
?>
Try this one :)
/<x:object\*([0-9]+)\/>/
better use
/<x:object\*([0-9]+)\/>/
精彩评论