bridge45
V2EX  ›  PHP

php 可以匹配"函数","类"么?求实现

  •  
  •   bridge45 · Aug 11, 2015 · 3033 views
    This topic created in 3932 days ago, the information mentioned may be changed or developed.

    <?php
    $str = <<<EOT
    function a(){
    echo 'fun a';
    }

    function b($b){
    echo 'b';
    }

    function c($c){
    if($c == 1){
    echo 'c1';
    }else{
    echo 'c2';
    }
    }
    EOT;

    要到达的效果如下:
    将字符串$str里面的三个函数分离出来存到数组里面去,有什么好的兼容性高的正则匹配方法么?(类同理)
    谢谢大家

    2 replies    2015-08-12 00:35:40 +08:00
    oott123
        1
    oott123  
       Aug 11, 2015 via Android
    这应该是个 tokenizer 做的事情?
    token_get_all
    msg7086
        2
    msg7086  
       Aug 12, 2015
    说个正则以外的办法:

    https://github.com/nikic/PHP-Parser PHP语法分析器。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   6182 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 02:15 · PVG 10:15 · LAX 19:15 · JFK 22:15
    ♥ Do have faith in what you're doing.