简体   繁体   中英

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 replace 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.
?>

试试这个:)

/<x:object\*([0-9]+)\/>/

更好地使用

/<x:object\*([0-9]+)\/>/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM