Jump to content

Daniel0

Staff Alumni
  • Posts

    11,885
  • Joined

  • Last visited

Everything posted by Daniel0

  1. For what it's worth, this will extract functions, classes and interfaces from a file. Took me ten minutes to write. I bet it took longer time writing those regular expressions. Sample file (test.php): <?php function foo() { echo 'hello world'; } echo 2 + 4; function hello () { // aslkjdlakjds return 'abc' + 1;} class Hello { function thisMethodWontGetIncludedInFunctions() { echo 'foo'; } } abstract class Foo {} final class Bar {} interface Baz {} echo 'more junk here'; ?> Parser: <?php class FileParser { private $_path; private $_parsed = false; private $_classes = array(); private $_functions = array(); private $_interfaces = array(); public function __construct($path) { $this->_path = $path; } private function _parse() { if ($this->_parsed) return; $parsing = null; $tmp = ''; $open = 0; foreach (token_get_all(file_get_contents($this->_path)) as $token) { if ($parsing === null && is_array($token)) { switch ($token[0]) { case T_FUNCTION: $parsing = T_FUNCTION; break; case T_CLASS: case T_ABSTRACT: case T_FINAL: $parsing = T_CLASS; break; case T_INTERFACE: $parsing = T_INTERFACE; break; } if ($parsing !== null) $tmp .= $token[1]; } else { if (is_array($token)) { $tmp .= $token[1]; } else { $tmp .= $token; switch ($token) { case '{': ++$open; break; case '}': if (--$open === 0) { switch ($parsing) { case T_FUNCTION: $this->_functions[] = $tmp; break; case T_CLASS: $this->_classes[] = $tmp; break; case T_INTERFACE: $this->_interfaces[] = $tmp; break; } $parsing = null; $tmp = ''; } break; } } } } $this->_parsed = true; } public function getClasses() { $this->_parse(); return $this->_classes; } public function getFunctions() { $this->_parse(); return $this->_functions; } public function getInterfaces() { $this->_parse(); return $this->_interfaces; } } $parser = new FileParser('test.php'); var_dump( $parser->getFunctions(), $parser->getClasses(), $parser->getInterfaces() ); Output: array(2) { [0]=> string(42) "function foo() { echo 'hello world'; }" [1]=> string(81) "+;function hello () { // aslkjdlakjds return 'abc' + 1;}" } array(3) { [0]=> string(95) "class Hello { function thisMethodWontGetIncludedInFunctions() { echo 'foo'; } }" [1]=> string(21) "abstract class Foo {}" [2]=> string(18) "final class Bar {}" } array(1) { [0]=> string(16) "interface Baz {}" }
  2. Well, it was not meant to be a complete solution for you. You were supposed to extend it in the same manner to capture classes. Take a look at how it works. When a function declaration starts it ignores the tokens and just adds them to the string until the function declaration ends. You can do the same with classes. When a class declaration starts, ignore everything until it ends.
  3. You know, there is no need to write a PHP parser yourself; there is already one in... PHP. Consider the following file (test.php): <?php function foo() { echo 'hello world'; } echo 2 + 4; function hello () { // aslkjdlakjds return 'abc' + 1;} echo 'more junk here'; ?> Then you can extract all the functions in that file like this: <?php $tokens = token_get_all(file_get_contents('test.php')); $started = false; $open = 0; $functions = array(); $tmp = ''; foreach ($tokens as $token) { if (!$started && $token[0] === T_FUNCTION) { $started = true; $tmp .= $token[1]; } else if ($started) { if (is_array($token)) { $tmp .= $token[1]; } else { $tmp .= $token; if ($token === '{') { ++$open; } else if ($token === '}') { if (--$open === 0) { $started = false; $functions[] = $tmp; $tmp = ''; } } } } } var_dump($functions); The output of running that would be: array(2) { [0]=> string(42) "function foo() { echo 'hello world'; }" [1]=> string(79) "function hello () { // aslkjdlakjds return 'abc' + 1;}" }
  4. Also have a look at http://php.net/foreach
  5. Oh really? Who would have known that?! Maybe you should read the topic before you reply so you don't repeat other people. PFMaBiSmAd said that in the second reply to this topic.
  6. COM is for Windows so it won't work on other platforms.
  7. How would redirecting the user take care of normalizing the string?
  8. Doing it on-the-fly takes no time. I just tried on a random MP3 file and it took 0.04 seconds completing. If you have a lot of users and are worried about spending a lot of resources, pre-generating them and storing them on the disk would be a good idea. You could implement a trimming function using PHP, but it would be no more efficient than using ffmpeg. Actually, it's unlikely it would be more efficient. Thanks for the idea paddy. Will try it out. That won't work unless you're willing to let anyone download the entire file. What's preventing them from disabling Javascript or just checking out your HTML code?
  9. Well, you could do if (strncmp($url, 'http://', 7) != 0) { $url = 'http://' . $url; } You're wrong about the www stuff. The domain name www.example.com doesn't need to point to the same place as example.com.
  10. Assuming that you've got ffmpeg installed, that command will take the first 30 seconds of original.mp3 and put them in trimmed.mp3.
  11. [abc]? will work just fine See: daniel@daniel-laptop:~$ cat test.php <?php $pattern = '/^[abc]?$/'; var_dump(preg_match($pattern, 'a')); var_dump(preg_match($pattern, '')); var_dump(preg_match($pattern, 'hello')); ?> daniel@daniel-laptop:~$ php test.php int(1) int(1) int(0) Quantifiers just operate on the previous subpattern and a character class qualifies as a subpattern.
  12. Which version of PHP are you using? Anonymous functions were added in PHP 5.3.0. If you're using an earlier version you'll have to upgrade.
  13. If you've got ffmpeg on your server, you can use it to do it. Something like this should do the trick: ffmpeg -t 30 -i original.mp3 -acodec copy trimmed.mp3
  14. Okay... How did you try it, and how does it "not work"? Help me help you. It's not like I can see what's going on on your computer, so vague statements like "it does not work" are entirely useless.
  15. <?php $array = array( "testInputElement" => array( "type" => "InputElement", "errorMessage" => "Please input a whole number between 30 and 90", "elementOptions" => array(), "validation" => function() {return false;} ), "testInputElement2" => array( "type" => "InputElement", "errorMessage" => "Error Message for form field 2 goes here", "elementOptions" => array(), "validation" => function() {return false;} ) ); var_dump($array); Gives no parse errors for me. Maybe you would like to enlighten us with what error you're getting?
  16. It will still match | (vertical bar) as a valid sign. Also, you don't need parentheses around that character class.
  17. Do like $_COOKIE[$cn] without the quotes around the variable name.
  18. The syntax error you're getting has nothing to do with the anonymous functions. Try matching up the opening parentheses with the closing parentheses. You have one more closing parenthesis than opening ones.
  19. correct! Heh... are you sure? <?php $sym_price = 'My name is Daniel| 5.00 hello world '; if (preg_match_all("/[-|+]\s\d+\.\d{2}/", $sym_price, $matches)) { echo 'It matches'; }
  20. Not tested, but how about this? /^[+-]?\d+\.\d+$/ Or if you only want the non-fractional part (is there a word for that) to be a multiple of 10, then perhaps like this: /^[+-]?10*\.\d+$/
  21. What exactly are you trying to do? Any reason why you can't use reflection to get information about classes?
  22. No, not necessarily. Only if you use the default settings. You may change the lifetime of the session cookie using the session.cookie_lifetime php.ini directive. You can set this (and other settings) using session_set_cookie_params. If you want it in a database (or somewhere else) instead, you can also use session_set_save_handler to accomplish that. Persisting logins are needed in virtually all applications, and that's why there is built-in support for it in PHP. That way everybody don't have to reinvent it all the time.
  23. Have a look at the operator precedence table. Type casting takes higher precedence than division.
  24. I can not see any HTML tag before DOCTYPE declaration. Then you need to look closer. [attachment deleted by admin]
  25. Any particular reason why you can't just use PHP's built-in session mechanism?
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.