代码之家  ›  专栏  ›  技术社区  ›  michaelmcgurk

在变量[duplicate]中获取值后的所有内容

php
  •  0
  • michaelmcgurk  · 技术社区  · 6 年前

    这个问题已经有了答案:

    $contents ,我想把烟斗后的东西都拿走。 boss .

    $search_for = "micky.mcgurk";
    $contents = "micky.mcgurk@somewebsite.com|boss";
    $pattern = sprintf('/\b%s@([^|\s]+)\|/m', preg_quote($search_for));
    
    if (preg_match_all($pattern, $contents, $matches)) {
        $submitted_by = implode("\n", $matches[1]);
    } else {
       echo "No user found";
    }
    
    4 回复  |  直到 6 年前
        1
  •  4
  •   CyrielleDev    6 年前
    $resultat = substr(strrchr($content, '|'), 1);
    
        2
  •  1
  •   SiL3NC3    6 年前

    为什么不直接用爆炸?

    $contents = "micky.mcgurk@somewebsite.com|boss";
    $result = explode("|", $contents);
    echo $result[1];
    

    这就要求,邮件地址和“”后面的内容没有“”也…;)

        3
  •  1
  •   Sergej    6 年前

    ReXEP方式:

    $pipe = preg_quote('|', '/');
    $regExp = '/.*' . $pipe . '(.*)$/';
    

    最快的方法:

    $name = substr($string, strpos($string, '|') + 1);
    
        4
  •  1
  •   user3783243    6 年前

    要回答当前regex的情况:

    ([^|\s]+)
    

    匹配每个连续的非 | 和空白字符。一旦遇到,它就会停止。因为抓捕组 () 被俘虏了。

    https://regex101.com/r/DVY3F0/2/

    不过,你想要的是在那之后。如果添加:

    \|\K.*
    

    并删除您希望在 0 索引。

    https://regex101.com/r/DVY3F0/3/

    完整形式:

    \bmicky\.mcgurk@[^|\s]+\|\K.*
    

    或者你可以把它作为一张模糊的支票 γ 直到一个 γ .

    [^|]+?\|\K.*
    

    https://regex101.com/r/yvzdDu/1/