V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Andor_Chen
V2EX  ›  问与答

求助:PHP 正则表达式

  •  
  •   Andor_Chen · Mar 11, 2012 · 5575 views
    This topic created in 5171 days ago, the information mentioned may be changed or developed.
    对正则表达式实在头大,自己搞了一会还是没出结果,所以请教一下各位达人。

    目的:

    从字符串中取出特定函数的第一个参数值

    $str = <form action="" method="post" onclick="<?php function_name('arg1', 'arg2' );?>>

    需要取出 function_name 的第一个参数值(arg1)。

    需要考虑的问题,1)函数名后的 ( 前后都可能有空格;2)参数调用可能是单引号或双引号。

    求指教,多谢。
    3 replies    1970-01-01 08:00:00 +08:00
    Vim
        1
    Vim  
       Mar 11, 2012
    你先确定一下你这代码能跑,然后重新组织一下语言
    Natsuki
        2
    Natsuki  
       Mar 11, 2012
    $str= <<<EOT
    <form action="" method="post" onclick=<?php function_name('arg1', 'arg2' );?>';
    EOT;
    $item = null;
    if(preg_match_all("/function_name.*?([\'\"])(.*?)\\1/",$str,$matches)){
    $item = $matches[2];
    }



    根据你的意思只能做到这样子
    Andor_Chen
        3
    Andor_Chen  
    OP
       Mar 11, 2012
    @Natsuki 十分感谢,it just works

    @Vim 以后会组织好语言
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1006 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 19:16 · PVG 03:16 · LAX 12:16 · JFK 15:16
    ♥ Do have faith in what you're doing.