开发者

regular expression to get sub string via php

开发者 https://www.devze.com 2023-02-11 10:07 出处:网络
How can I use a reg开发者_运维知识库ular expression in the substr() PHP function to get a substring matched by a pattern?

How can I use a reg开发者_运维知识库ular expression in the substr() PHP function to get a substring matched by a pattern?

Edited:

example:

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

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


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

Update:

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

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


Try this:

$matches = array();
preg_match("/\[([^]]*)\]/", 'hello [*kitty*],how good is today', $matches);
print_r($matches);

Oops, fixed it now :)

0

精彩评论

暂无评论...
验证码 换一张
取 消