简体   繁体   English

PHP正则表达式-从字符串获取数字

[英]PHP Regular Expression - Get number from string

I have a string sequence that goes like this: <x:object*10/> . 我有一个像这样的字符串序列: <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. 我将上面的标记与/<x:object\\*[0-9]+\\/>/匹配,并使用preg_replace可以将其全部替换为所需的内容。 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"... 它将以名称“ 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]+)\/>/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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