正则表达式通过php获取子字符串 [英] regular expression to get sub string via php

查看:319
本文介绍了正则表达式通过php获取子字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在substr() PHP函数中使用正则表达式来获取与模式匹配的子字符串?

How can I use a regular expression in the substr() PHP function to get a substring matched by a pattern?

示例:

$name = 'hello [*kitty*],how good is today';

我想获取[ .... ]占位符之间的含义.

I want to get what is between [....] placeholder.

推荐答案

substr()仅匹配整个字符串.您正在寻找 preg_match() .

substr() only matches whole strings. You are looking for preg_match().

更新:

$name = 'hello [*kitty*],how good is today';
preg_match( '/\[(.*?)\]/', $name, $match );
var_dump( $match );

您可以在$match[1]中找到该名称.我建议您阅读正则表达式以了解preg_match().

You can find the name in $match[1]. I suggest you read up on regular expressions to understand preg_match().

这篇关于正则表达式通过php获取子字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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