如何使用 preg_match 提取数据? [英] How to use preg_match to extract data?

查看:47
本文介绍了如何使用 preg_match 提取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 preg_match 的使用还很陌生.在发布这个问题之前搜索了很多答案.找到了很多帖子以根据 youtube ID 等获取数据.但没有满足我的需求.如果是愚蠢的问题,请原谅我.

I am pretty new to the use of preg_match. Searched a lot for an answer before posting this question. Found a lot of posts to get data based on youtube ID etc. But nothing as per my needs. If its silly question, please forgive me.

我需要从带有 preg_match 的字符串中获取 ID.字符串格式为

I need to get the ID from a string with preg_match. the string is in the format

[#1234] Subject

如何只从字符串中提取1234"?

How can I extract only "1234" from the string?

推荐答案

一种解决方案是:

\[#(\d+)\]

这匹配左方括号和井号 [#,然后捕获一个或多个数字,然后是右方括号 ].

This matches the left square bracket and pound sign [#, then captures one or more digits, then the closing right square bracket ].

你会像这样使用它:

preg_match( '/\[#(\d+)\]/', '[#1234] Subject', $matches);
echo $matches[1]; // 1234

您可以在这个演示中看到它的运行情况.

You can see it working in this demo.

这篇关于如何使用 preg_match 提取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆