regular expression to get sub string via php

前端 未结 2 1605
无人及你
无人及你 2021-01-04 05:48

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

Edited:

example:

$name          


        
相关标签:
2条回答
  • 2021-01-04 06:06

    Try this:

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

    Oops, fixed it now :)

    0 讨论(0)
  • 2021-01-04 06:20

    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().

    0 讨论(0)
提交回复
热议问题