如何在PHP中从Regex获取变量?

时间:2022-11-27 21:06:26

How can I get only the Name/Variable which is "regexed"? Like in this case the $1 or $0 in the anchor's href?

我怎样才能获得“regexed”的名称/变量?就像在这种情况下,锚点的href中的$ 1或$ 0?

When I try to echo the $1 or $0 I get a Syntax Error because it's a Number. At the Moment the $str is a whole Text.

当我尝试回显$ 1或$ 0时,我得到一个语法错误,因为它是一个数字。在片刻,$ str是一个完整的文本。

function convertHashtags($str){
    $regex = "/#+([a-zA-Z0-9_]+)/";
    $str = preg_replace($regex, '<a href="hashtag.php?tag=$1">$0</a>', $str);
    return($str);
    }

1 个解决方案

#1


6  

Simple use preg_match before preg_replace, eg

在preg_replace之前简单使用preg_match,例如

preg_match($regex, $str, $matches);

Assuming the pattern actually matched, you should have the results in $matches[0] and $matches[1] which are the equivalent of $0 and $1 in the replace string.

假设模式实际匹配,你应该在$ matches [0]和$ matches [1]中得到结果,它们相当于替换字符串中的$ 0和$ 1。

FYI, the $n tokens in the replacement string are not variables though I can see how that can be confusing. They are simply references to matched groups (or the entire match in the case of $0) in the regex.

仅供参考,替换字符串中的$ n令牌不是变量,但我可以看出这可能令人困惑。它们只是在正则表达式中引用匹配的组(或者在$ 0的情况下的整个匹配)。

See http://php.net/manual/function.preg-replace.php#refsect1-function.preg-replace-parameters


To find multiple matches in $str, use preg_match_all(). It's almost the same only it populates $matches with a collection of matches. Use the PREG_SET_ORDER flag as the 4th argument to make the array workable. For example...

要在$ str中查找多个匹配项,请使用preg_match_all()。它只是用一系列匹配来填充$匹配。使用PREG_SET_ORDER标志作为第4个参数使数组可用。例如...

$str = ' xD #lol and #testing';
$regex = '/#(\w+)/';
preg_match_all($regex, $str, $allMatches, PREG_SET_ORDER);
print_r($allMatches);

produces...

Array
(
    [0] => Array
        (
            [0] => #lol
            [1] => lol
        )

    [1] => Array
        (
            [0] => #testing
            [1] => testing
        )

)

#1


6  

Simple use preg_match before preg_replace, eg

在preg_replace之前简单使用preg_match,例如

preg_match($regex, $str, $matches);

Assuming the pattern actually matched, you should have the results in $matches[0] and $matches[1] which are the equivalent of $0 and $1 in the replace string.

假设模式实际匹配,你应该在$ matches [0]和$ matches [1]中得到结果,它们相当于替换字符串中的$ 0和$ 1。

FYI, the $n tokens in the replacement string are not variables though I can see how that can be confusing. They are simply references to matched groups (or the entire match in the case of $0) in the regex.

仅供参考,替换字符串中的$ n令牌不是变量,但我可以看出这可能令人困惑。它们只是在正则表达式中引用匹配的组(或者在$ 0的情况下的整个匹配)。

See http://php.net/manual/function.preg-replace.php#refsect1-function.preg-replace-parameters


To find multiple matches in $str, use preg_match_all(). It's almost the same only it populates $matches with a collection of matches. Use the PREG_SET_ORDER flag as the 4th argument to make the array workable. For example...

要在$ str中查找多个匹配项,请使用preg_match_all()。它只是用一系列匹配来填充$匹配。使用PREG_SET_ORDER标志作为第4个参数使数组可用。例如...

$str = ' xD #lol and #testing';
$regex = '/#(\w+)/';
preg_match_all($regex, $str, $allMatches, PREG_SET_ORDER);
print_r($allMatches);

produces...

Array
(
    [0] => Array
        (
            [0] => #lol
            [1] => lol
        )

    [1] => Array
        (
            [0] => #testing
            [1] => testing
        )

)