Use PHP custom protocol to convert string code into executable code

Source: Internet
Author: User
Take the parsing annotation configuration in crossphp as an example to define the protocol name: annotate $ str_codearray (foobar); stream_register_wrapper (annotate, stringToPHPStream); $ varinclude (annotate: {$ str_code }); print_r ($ var); the string $ str_code becomes a php array. Stay away from eval, cool.

Take the parsing annotation configuration in crossphp as an example to define the protocol name: annotate $ str_code = array (foo = bar); stream_register_wrapper (annotate, stringToPHPStream); $ var = include (annotate: // {$ str_code}); print_r ($ var); then the string $ str_code becomes a php array. Stay away from eval, cool.

Take the parsing annotation configuration in crossphp as an example to define the protocol name: annotate

$ Str_code = 'array ("foo" => "bar ")';
Stream_register_wrapper ("annotate", "stringToPHPStream ");
$ Var = include ("annotate: // {$ str_code }");

Print_r ($ var );
In this way, the string $ str_code becomes a php array. Stay away from eval, cool ~
To change the protocol name in the example, change the stream_open method CrossPHP in the stringToPHPStream class.
 "bar")';stream_register_wrapper("annotate", "stringToPHPStream");$var = include ("annotate://{$str_code}");print_r($var);
 * Usage method stream_register_wrapper ("custom name", "stringToPHPStream") * $ var = include ("custom name: // string Code ")*
** @ Auth: wonli * Class StringToPHPStream * @ package Cross \ Lib \ Other */class StringToPHPStream {/*** code content ** @ var array */static $ content; /*** mark in $ content ** @ var string */protected $ key;/*** @ var int */protected $ pos; /*** @ param $ path * @ param $ mode * @ param $ options * @ param $ opened_path * @ return bool */public function stream_open ($ path, $ mode, $ options, $ opened_path) {$ this-> key = md5 ($ path ); If (! Isset (self ::$ content [$ this-> key]) {self ::$ content [$ this-> key] = sprintf (' Pos = 0; return true;}/*** @ param $ count * @ return string */public function stream_read ($ count) {$ content = self :: $ content [$ this-> key]; $ ret = substr ($ content, $ this-> pos, $ count ); $ this-> pos + = strlen ($ ret); return $ ret;}/*****/public function stream_stat () {}/ ****/public function stream_eof (){}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.