preg-replace-callback

How to implement my algorithm text correction for the replacement of words in the text?

馋奶兔 提交于 2019-12-06 07:33:54
问题 Brief Help me to create a new function or change the function correct() so that the result works in a case-insensitive manner for the input text. Example Usage Example usage for the correct() method: $text = "Точик ТОЧИК точик ТоЧиК тоЧИК"; $text = correct($text, $base_words); echo "$text"; Expected Result Input: Точик ТОЧИК точик ТоЧиК тоЧИК Output: Тоҷик ТОҶИК тоҷик ТоҶиК тоҶИК Code Here are all the arrays and functions below so you can easily copy them: $default_words = array ( 'бур',

Custom parsing function PHP

只愿长相守 提交于 2019-12-05 23:08:22
I'm trying to remove eval from the following function. I tried with sprintf and ${} , but still cannot find a solution. Here the function: function parseDbString(string $value = 'Looking for a good {{ $pippo }}'){ $pippo='Pizza'; return preg_replace_callback('/{{(.*?)}}/', function($res) use ($pippo) { // $val=${trim($res[1])}; Returns "Undefined variable: $pippo" $val=@eval("return ".trim($res[1]).";"); // Returns "Looking for a good Pizza" return isset($val) ? $val : $res[0]; },$value); } So, yes, eval() is often detested as one of the highest order "evils" in php. In most cases, when a task

preg_replace with multiple patterns replacements at once

孤街醉人 提交于 2019-12-05 02:50:39
问题 I have few substitutions to apply on my $subject but I don't want to allow the output from old substitutions #(1 .. i-1) to be a match for the current substitution #i. $subject1 = preg_replace($pat0, $rep0, $subject0); $subject2 = preg_replace($pat1, $rep1, $subject1); $subject3 = preg_replace($pat2, $rep2, $subject2); I tried using one preg_replace with arrays for patterns and replacement hoping that it make it at once; but it turned out to be not more than calling the simple preg_replace

Second parameter in preg_replace_callback()

北城以北 提交于 2019-12-04 21:32:40
I have a problem with the function preg_replace_callback() in PHP. I want to call a function which requires two parameters. private function parse_variable_array($a, $b) { return $a * $b; } On the internet I found this piece of code: preg_replace_callback("/regexcode/", call_user_func_array(array($this, "foo"), array($foo, $bar)), $subject); But in the function foo I cannot use the matches array that is usual with a preg_replace_callback I hope you can help me! The callback is called as is, you cannot pass additional parameters to it. You can make a simple wrapper function though. For PHP 5.3+

How to implement my algorithm text correction for the replacement of words in the text?

风格不统一 提交于 2019-12-04 12:11:23
Brief Help me to create a new function or change the function correct() so that the result works in a case-insensitive manner for the input text. Example Usage Example usage for the correct() method: $text = "Точик ТОЧИК точик ТоЧиК тоЧИК"; $text = correct($text, $base_words); echo "$text"; Expected Result Input: Точик ТОЧИК точик ТоЧиК тоЧИК Output: Тоҷик ТОҶИК тоҷик ТоҶиК тоҶИК Code Here are all the arrays and functions below so you can easily copy them: $default_words = array ( 'бур', 'кори', 'давлати', 'забони', 'фанни' ); $base_words = array ( "точик" => "тоҷик", "точики" => "тоҷики",

Case insensitive preg_replace_callback

末鹿安然 提交于 2019-12-04 06:18:22
问题 In the function below, I want to match the keyword case insensitive (should match "Blue Yoga Mats" and "blue yoga mats")... However, it currently only matches if the keyword is the same case. $mykeyword = "Blue Yoga Mats"; $post->post_content = preg_replace_callback("/\b($mykeyword)\b/","doReplace", $post->post_content); // the callback function function doReplace($matches) { static $count = 0; // switch on $count and later increment $count. switch($count++) { case 0: return '<b>'.$matches[1]

preg_replace with multiple patterns replacements at once

巧了我就是萌 提交于 2019-12-03 20:18:16
I have few substitutions to apply on my $subject but I don't want to allow the output from old substitutions #(1 .. i-1) to be a match for the current substitution #i. $subject1 = preg_replace($pat0, $rep0, $subject0); $subject2 = preg_replace($pat1, $rep1, $subject1); $subject3 = preg_replace($pat2, $rep2, $subject2); I tried using one preg_replace with arrays for patterns and replacement hoping that it make it at once; but it turned out to be not more than calling the simple preg_replace successively (with some optimization of course) After I read about preg_replace_callback , I guess it is

How to catch nested {% if … %}{% endif %} statments with regex

独自空忆成欢 提交于 2019-12-02 23:35:20
问题 This is what I got now: /{% if(.+?) %}(.*?){% endif %}/gusi It catches multiple if statements etc just fine. IMG: http://image.xesau.eu/2015-02-07_23-22-11.png But when I do nested ones, so an if in an if, it stops at the first occurence of {% endif %} IMG: http://image.xesau.eu/2015-02-08_09-29-43.png Is there a way catch as many {% endif %} statements as there were {% if ... %} statements, and if so, how? 回答1: Don't use regexen, use the existing Twig parser. Here's a sample of an extractor

Convert a function from preg_replace to preg_replace_callback()

烂漫一生 提交于 2019-12-02 22:25:43
问题 I need to convert preg_replace() to preg_replace_callback() in this function of an outdated CMS extension: // santizes a regex pattern private static function sanitize( $pattern, $m = false, $e = false ) { if( preg_match( '/^\/(.*)([^\\\\])\/(.*?)$/', $pattern, $matches ) ) { $pat = preg_replace( '/([^\\\\])?\(\?(.*\:)?(.*)\)/Ue', '\'$1(?\' . self::cleanupInternal(\'$2\') . \'$3)\'', $matches[1] . $matches[2] ); $ret = '/' . $pat . '/'; if( $m ) { $mod = ''; foreach( self::$modifiers as $val

How to catch nested {% if … %}{% endif %} statments with regex

你离开我真会死。 提交于 2019-12-02 14:23:54
This is what I got now: /{% if(.+?) %}(.*?){% endif %}/gusi It catches multiple if statements etc just fine. IMG: http://image.xesau.eu/2015-02-07_23-22-11.png But when I do nested ones, so an if in an if, it stops at the first occurence of {% endif %} IMG: http://image.xesau.eu/2015-02-08_09-29-43.png Is there a way catch as many {% endif %} statements as there were {% if ... %} statements, and if so, how? Don't use regexen, use the existing Twig parser. Here's a sample of an extractor I wrote which parses for custom tags and extracts them: https://github.com/deceze/Twig-extensions/tree