コード例 #1
0
ファイル: xt_phptpl_lib.php プロジェクト: sunnybutani/webc
 function xthreads_phptpl_parsetpl(&$ourtpl, $fields = array(), $evalvarname = null)
 {
     $GLOBALS['__phptpl_if'] = array();
     $repl = array('#\\<((?:else)?if\\s+(.*?)\\s+then|else\\s*/?|/if)\\>#si' => function ($m) use($fields) {
         return xthreads_phptpl_if($m[1], _xthreads_phptpl_expr_parse2($m[2], $fields));
     }, '#\\<func (htmlspecialchars|htmlspecialchars_uni|intval|floatval|urlencode|rawurlencode|addslashes|stripslashes|trim|crc32|ltrim|rtrim|chop|md5|nl2br|sha1|strrev|strtoupper|strtolower|my_strtoupper|my_strtolower|alt_trow|get_friendly_size|filesize|strlen|my_strlen|my_wordwrap|random_str|unicode_chr|bin2hex|str_rot13|str_shuffle|strip_tags|ucfirst|ucwords|basename|dirname|unhtmlentities)\\>#i' => function ($m) {
         return '".' . $m[1] . '("';
     }, '#\\</func\\>#i' => function () {
         return '")."';
     }, '#\\<\\?=(.*?)\\?\\>#s' => function ($m) use($fields) {
         return '".strval(' . _xthreads_phptpl_expr_parse2($m[1], $fields) . ')."';
     }, '#\\<setvar\\s+([a-z0-9_\\-+!(),.]+)\\>(.*?)\\</setvar\\>#i' => function ($m) use($fields) {
         return '".(($GLOBALS["tplvars"][\'' . $m[1] . '\'] = (' . _xthreads_phptpl_expr_parse2($m[2], $fields) . '))?"":"")."';
     });
     if ($evalvarname) {
         $repl['#\\<while\\s+(.*?)\\s+do\\>#si'] = function ($m) use($fields, $evalvarname) {
             return '"; while(' . _xthreads_phptpl_expr_parse2($m[1], $fields) . ') { $' . $evalvarname . '.="';
         };
         $repl['#\\<foreach\\s+(.*?)\\s+do\\>#si'] = function ($m) use($fields, $evalvarname) {
             return '"; foreach(' . _xthreads_phptpl_expr_parse2($m[1], $fields) . ' as $__key => $__value) { $' . $evalvarname . '.="';
         };
         $repl['#\\<repeat\\s+(.*?)\\s+do\\>#si'] = function ($m) use($fields, $evalvarname) {
             return '"; for($__iter=0; $__iter < ' . _xthreads_phptpl_expr_parse2($m[1], $fields) . '; ++$__iter) { $' . $evalvarname . '.="';
         };
         $repl['#\\</(while|foreach|repeat)\\>#i'] = function ($m) use($evalvarname) {
             return '"; } $' . $evalvarname . '.="';
         };
     }
     if (xthreads_allow_php()) {
         $repl['#\\<\\?(?:php|\\s).+?(\\?\\>)#s'] = function ($m) use($fields) {
             return xthreads_phptpl_evalphp(_xthreads_phptpl_expr_parse2($m[0], $fields), $m[1]);
         };
     }
     $ourtpl = preg_replace_callback_array($repl, $ourtpl);
 }
コード例 #2
0
ファイル: Php70Test.php プロジェクト: remicollet/polyfill
 public function testPregReplaceCallbackArray()
 {
     $this->assertSame('ddda', preg_replace_callback_array(array('/[^a]/' => function () {
         return 'a';
     }, '/a/' => function () {
         return 'd';
     }), 'abca', 3, $count));
     $this->assertSame(5, $count);
 }
コード例 #3
0
ファイル: Traffic.php プロジェクト: silawrenc/traffic
 public function route(string $method, string $uri) : bool
 {
     $req = $method . ' ' . $uri;
     if ($this->compiled === null) {
         $this->compile();
     }
     foreach ($this->compiled as $regex => $routes) {
         if (preg_match($regex, $req) === 1) {
             preg_replace_callback_array($routes, $req, 1);
             return true;
         }
     }
     return false;
 }
コード例 #4
0
ファイル: phptpl.php プロジェクト: sammykumar/TheVRForums
 function phptpl_parsetpl(&$ourtpl)
 {
     $GLOBALS['__phptpl_if'] = array();
     $ourtpl = preg_replace_callback_array(array('#\\<((?:else)?if\\s+(.*?)\\s+then|else\\s*/?|/if)\\>#si' => function ($m) {
         return phptpl_if($m[1], phptpl_unescape_string($m[2]));
     }, '#\\<func (htmlspecialchars|htmlspecialchars_uni|intval|floatval|urlencode|rawurlencode|addslashes|stripslashes|trim|crc32|ltrim|rtrim|chop|md5|nl2br|sha1|strrev|strtoupper|strtolower|my_strtoupper|my_strtolower|alt_trow|get_friendly_size|filesize|strlen|my_strlen|my_wordwrap|random_str|unicode_chr|bin2hex|str_rot13|str_shuffle|strip_tags|ucfirst|ucwords|basename|dirname|unhtmlentities)\\>#i' => function ($m) {
         return '".' . $m[1] . '("';
     }, '#\\</func\\>#i' => function () {
         return '")."';
     }, '#\\<template\\s+([a-z0-9_ \\-+!(),.]+)(\\s*/)?\\>#i' => function ($m) {
         return $GLOBALS['templates']->get($m[1]);
     }, '#\\<\\?=(.*?)\\?\\>#s' => function ($m) {
         return '".strval(' . phptpl_unescape_string($m[1]) . ')."';
     }, '#\\<setvar\\s+([a-z0-9_\\-+!(),.]+)\\>(.*?)\\</setvar\\>#i' => function ($m) {
         return '".(($GLOBALS["tplvars"][\'' . $m[1] . '\'] = (' . phptpl_unescape_string($m[2]) . '))?"":"")."';
     }, '#\\<\\?(?:php|\\s).+?(\\?\\>)#s' => function ($m) {
         return phptpl_evalphp(phptpl_unescape_string($m[0]), $m[1], false);
     }), $ourtpl);
 }
コード例 #5
0
ファイル: ConfigParser.php プロジェクト: blacksmoke26/yii2cdn
 /**
  * Replaces tags (starts with @) from file name<br><br>
  * Rules: (no url appends at beginning if)<br>
  * <code>
  *    > An actual url
  *    > starts with //
  * </code><br>
  * Supported tags (case insensitive)<br>
  * <code>
  *    > @appUrl : current application url
  *    > @baseUrl : component base url
  *    > @thisSectionUrl : parent section base url
  *    > @thisSectionPath : parent section base path
  *    > @url (*) : * = any url ends with /
  *    > @alias (*) : * = CDN custom alias name
  *    > @yiiAlias (*) : * = Yii alias name
  * </code><br>
  * @param string $fileName Replace tags from filename
  * @param string $type The section name (or type)
  * @return string
  */
 protected function replaceFileNameTags($fileName, $type)
 {
     if (\strpos($fileName, '//') === 0 || \filter_var($fileName, \FILTER_VALIDATE_URL)) {
         return $fileName;
     }
     // Replace tags
     if (false !== strpos($fileName, '@')) {
         $patterns = ['/^((?i)@thisComponentUrl(?-i))(.+)$/' => function ($match) use($type) {
             return $this->getUrl() . '/' . ltrim($match[2], '/');
         }, '/^((?i)@thisSectionUrl(?-i))(.+)$/' => function ($match) use($type) {
             return $this->getSectionUrl($type, $match[2]);
         }, '/^(?i)@alias(?-i)\\(([^\\)]+)\\)(.+)?$/' => function ($match) {
             if (!array_key_exists($match[1], $this->aliases)) {
                 throw new InvalidConfigException("Invalid custom url alias '{$match[1]}' given");
             }
             return $this->aliases[$match[1]] . '/' . ltrim($match[2], '/');
         }, '/^(?i)@yiiAlias(?-i)\\(([^\\)]+)\\)(.+)$/' => function ($match) {
             return \Yii::getAlias($match[1]) . '/' . ltrim($match[2], '/');
         }, '/^(?i)@url(?-i)\\(([^\\)]+)\\)(.+)$/' => function ($match) {
             return $match[1] . '/' . ltrim($match[2], '/');
         }, '/^((?i)@appUrl(?-i))(.+)$/' => function ($match) {
             return $this->baseUrl . '/' . ltrim($match[2], '/');
         }, '/^((?i)@baseUrl(?-i))(.+)$/' => function ($match) {
             return $this->getUrl() . '/' . ltrim($match[2], '/');
         }];
         $output = rtrim(preg_replace_callback_array($patterns, $fileName), '/');
         // tag: @*** remove
         return preg_replace('/^(@[a-zA-Z]+\\([a-zA-Z0-9_]+\\)|@([a-zA-Z]+))/', '', $output);
     }
     return $this->getSectionUrl($type, $fileName);
 }
コード例 #6
0
<?php

/**
 * http://php.net/manual/en/function.preg-replace-callback-array.php
 */
$subject = 'Aaaaaa Bbb';
preg_replace_callback_array(['~[a]+~i' => function ($match) {
    echo strlen($match[0]), ' matches for "a" found', PHP_EOL;
}, '~[b]+~i' => function ($match) {
    echo strlen($match[0]), ' matches for "b" found', PHP_EOL;
}], $subject);
コード例 #7
0
<?php

$list = <<<END
<p class=ListParaFirst><span
style='font-family:Symbol;'>·<span style='font:7.0pt "Times New Roman"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span>APPLES</p>
  <p class=ListParaMiddle><span
style='font-family:Symbol;'>·<span style='font:7.0pt "Times New Roman"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span>BANANAS</p>
  <p class=ListParaMiddle><span
style='font-family:Symbol;'>·<span style='font:7.0pt "Times New Roman"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span>ORANGES</p>
  <p class=ListParaLast><span
style='font-family:Symbol;'>·<span style='font:7.0pt "Times New Roman"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </span></span>PEARS</p>
END;
$list_start = '<ul>';
$list_end = '</ul>';
$item_start = '<li>';
$item_end = '</li>';
// Match the last bullet point, and wrap the text
// in a pair of <li> tags followed by closing </ul>
function lastBullet($m)
{
    return "<li>{$m['1']}</li>" . PHP_EOL . '</ul>';
}
$list = preg_replace_callback_array(array('{</span>(\\w+)</p>}m' => function ($m) use($item_end) {
    return ucfirst(strtolower($m[1])) . $item_end;
}, '{<p class=ListParaFirst[\\s\\S]*?</span>}m' => function ($m) use($list_start, $item_start) {
    return $list_start . PHP_EOL . $item_start;
}, '{<p class=ListParaMiddle[\\s\\S]*?</span>}m' => function ($m) use($item_start) {
    return $item_start;
}, '{<p class=ListParaLast[\\s\\S]*?</span>(\\w+)</li>}m' => 'lastBullet'), $list);
echo $list;
コード例 #8
0
<?php

$htmlString = preg_replace_callback_array(['/(href="?)(\\S+)("?)/i' => function (&$matches) {
    return $matches[1] . urldecode($matches[2]) . $matches[3];
}, '/(href="?\\S+)(%24)(\\S+)?"?/i' => function (&$matches) {
    return urldecode($matches[1] . '$' . $matches[3]);
}], $htmlString);
コード例 #9
0
<?php

// This example is taken from the PHP reference docs for the
// preg_replace_callback_array() function, with slight modification.
$subject = 'Aaafaaba BbaCZbx';
$count = 0;
$ret = preg_replace_callback_array(['~[a]+~i' => function ($match) {
}, '~[b]+~i' => function ($match) {
}], $subject, -1, $count);
var_dump($ret);
var_dump($count);
コード例 #10
0
<?php

class Rep
{
    public function __invoke()
    {
        return "d";
    }
}
class Foo
{
    public static function rep($rep)
    {
        return "ok";
    }
}
function b()
{
    return "b";
}
var_dump(preg_replace_callback_array(array("/a/" => 'b', "/b/" => function () {
    return "c";
}, "/c/" => new Rep(), '/d/' => array("Foo", "rep")), 'a'));
var_dump(preg_replace_callback_array(array("/a/" => 'b', "/c/" => new Rep(), "/b/" => function () {
    return "ok";
}, '/d/' => array("Foo", "rep")), 'a'));
var_dump(preg_replace_callback_array(array('/d/' => array("Foo", "rep"), "/c/" => new Rep(), "/a/" => 'b', "/b/" => create_function('$a', 'return "ok";')), 'a', -1, $count));
var_dump($count);
コード例 #11
0
function _DoHeaders($text)
{
    # Setext-style headers:
    #	  Header 1
    #	  ========
    #
    #	  Header 2
    #	  --------
    #
    if (version_compare(phpversion(), '7.0.0', '<')) {
        $text = preg_replace(['{ ^(.+)[ \\t]*\\n=+[ \\t]*\\n+ }emx', '{ ^(.+)[ \\t]*\\n-+[ \\t]*\\n+ }emx'], ["'<h1>'._RunSpanGamut(_UnslashQuotes('\\1')).'</h1>\n\n'", "'<h2>'._RunSpanGamut(_UnslashQuotes('\\1')).'</h2>\n\n'"], $text);
    } else {
        $text = preg_replace_callback_array(['{ ^(.+)[ \\t]*\\n=+[ \\t]*\\n+ }mx' => function ($matches) {
            return '<h1>' . _RunSpanGamut(_UnslashQuotes($matches[1])) . "</h1>\n\n";
        }, '{ ^(.+)[ \\t]*\\n-+[ \\t]*\\n+ }mx' => function ($matches) {
            return '<h2>' . _RunSpanGamut(_UnslashQuotes($matches[1])) . "</h2>\n\n";
        }], $text);
    }
    # atx-style headers:
    #	# Header 1
    #	## Header 2
    #	## Header 2 with closing hashes ##
    #	...
    #	###### Header 6
    #
    preg_replace_callback("{\n\t\t\t^(\\#{1,6})\t# \$1 = string of #'s\n\t\t\t[ \\t]*\n\t\t\t(.+?)\t\t# \$2 = Header text\n\t\t\t[ \\t]*\n\t\t\t\\#*\t\t\t# optional closing #'s (not counted)\n\t\t\t\\n+\n\t\t}xm", function ($matches) {
        return '<h' . strlen($matches[1]) . '>' . _RunSpanGamut(_UnslashQuotes($matches[2])) . '</h' . strlen($matches[1]) . '>\\n\\n';
    }, $text);
    return $text;
}
コード例 #12
0
<?php

// 기존 preg_replace_callback 메서드의 callback 파라미터가 하나만 지원해서 가독성이 떨어짐
// 다수의 callback을 사용하기 위해 preg_replace_callback_array 메서드 추가
$subject = 'ABC DEF GH IJK';
echo preg_replace_callback_array(array("/ABC/m" => function ($matches) {
    return "a_b_c";
}, "/DEF/m" => function ($matches) {
    return "D E F";
}, "/GH/m" => function ($matches) {
    return "g h";
}), $subject);
コード例 #13
0
<?php

## Функция preg_replace_callback_array().
$str = '<hTmL><bOdY>Hello world!</bOdY></html>';
$str = preg_replace_callback_array(['{(?<btag></?)(?<content>\\w+)(?<etag>.*?>)}s' => function ($m) {
    return $m['btag'] . strtoupper($m['content']) . $m['etag'];
}, '{(?<=>)([^<>]+?)(?=<)}s' => function ($m) {
    return "<strong>{$m['1']}</strong>";
}], $str);
echo htmlspecialchars($str);
<?php

var_dump(preg_replace_callback_array());
var_dump(preg_replace_callback_array(1));
var_dump(preg_replace_callback_array(1, 2));
var_dump(preg_replace_callback_array(1, 2, 3));
$a = 5;
var_dump(preg_replace_callback_array(1, 2, 3, $a));
$a = "";
var_dump(preg_replace_callback_array(array("" => ""), "", "", $a));
$a = array();
$b = "";
var_dump(preg_replace_callback($a, $a, $a, $a, $b));
var_dump($b);
$b = "";
var_dump(preg_replace_callback_array(array("xx" => "s"), $a, -1, $b));
var_dump($b);
function f()
{
    static $count = 1;
    throw new Exception($count);
}
var_dump(preg_replace_callback_array(array('/\\w' => 'f'), 'z'));
try {
    var_dump(preg_replace_callback_array(array('/\\w/' => 'f', '/.*/' => 'f'), 'z'));
} catch (Exception $e) {
    var_dump($e->getMessage());
}
echo "Done\n";
コード例 #15
0
<?php

// Copyright (c) 2015 Thomas Punt
// https://github.com/tpunt/PHP7-Reference/blob/master/LICENSE.md
// https://github.com/tpunt/PHP7-Reference#preg_replace_callback_array-function
$tokenStream = [];
// [tokenName, lexeme] pairs
$input = <<<'end'
$a = 3; // variable initialisation
end;
$ret = preg_replace_callback_array(['~\\$[a-z_][a-z\\d_]*~i' => function ($match) use(&$tokenStream) {
    $tokenStream[] = ['T_VARIABLE', $match[0]];
}, '~=~' => function ($match) use(&$tokenStream) {
    $tokenStream[] = ['T_ASSIGN', $match[0]];
}, '~[\\d]+~' => function ($match) use(&$tokenStream) {
    $tokenStream[] = ['T_NUM', $match[0]];
}, '~;~' => function ($match) use(&$tokenStream) {
    $tokenStream[] = ['T_TERMINATE_STMT', $match[0]];
}, '~//.*~' => function ($match) use(&$tokenStream) {
    $tokenStream[] = ['T_COMMENT', $match[0]];
}], $input);
var_dump($ret);
var_dump($tokenStream);