preg-replace-callback

Replace curly braced expression with one item from the expression

蹲街弑〆低调 提交于 2021-02-16 20:30:17
问题 Here is a sample string: {three / fifteen / one hundred} this is the first random number, and this is the second, {two / four} From the brackets, I need to return a random value for example: One hundred is the first random number, and this is the second, two My code: function RandElement($str) { preg_match_all('/{([^}]+)}/', $str, $matches); return print_r($matches[0]); } $str = "{three/fifteen/one hundred} this is the first random number, and this is the second, {two/four}"; RandElement($str

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback

萝らか妹 提交于 2021-02-11 15:14:42
问题 I can not fix this error: $match[1] = preg_replace('/(?<=^|[a-z])./e', 'strtoupper("\0")', strtolower(trim($match[1]))); How to change it? 回答1: You should read the manual. The e modifier is deprecated and will be removed in further versions. Just use preg_replace_callback (the message told you..) $match[1] = preg_replace_callback('/(?<=^|[a-z])./', function($m) { return strtoupper($m[0]); }, strtolower(trim($match[1]))); 来源: https://stackoverflow.com/questions/33539495/deprecated-preg-replace

How to replace text with a regex pattern and integrate a counter in the replacement text?

牧云@^-^@ 提交于 2021-02-05 10:00:18
问题 function parse($string){ $counter = 0; $string = preg_replace("_\[b\](.*?)\[/b\]_si", '<span class="b">'. $counter .'. $1</span>', $string, -1, $counter); return $string; } I'm trying to make a ubb parser, that parses tags and put the counting in front of it: [b]Hey[/b] [b]Hello[/b] Should return this: <span class="b">1. Hey</span> <span class="b">2. Hello</span> But is returning this: <span class="b">1. Hey</span> <span class="b">1. Hello</span> So beside the function above, I've tried this:

Replace a string that matches a pattern using preg_replace_callback

主宰稳场 提交于 2021-01-27 21:41:59
问题 I want to replace the occurrences of the pattern "binary_function([x,y])" with substring "XY" in a given string. I have it working with the following code: // $string is the string to be searched $string = preg_replace_callback('/binary_function\(\[(\S),(\S)\]\)/', function ($word) { $result = strtoupper($word[1]) . strtoupper($word[2]); return $result; }, $string); However, I also want it to replace "binary_function([x1,y1])" with substring "X1Y1", and any length of the arguments inside the

Converting preg_replace to preg_replace_callback for finding and replacing words with variables

一笑奈何 提交于 2020-04-07 09:15:31
问题 I have the following line of code: $message = preg_replace('/\{\{([a-zA-Z_-]+)\}\}/e', "$$1", $body); This replaces words surrounded by two curly brackets with variables of the same name. ie {{username}} gets replaced by $username. I am trying to convert it to use preg_replace_callback. This is my code so far based on Googling, but I'm not really sure what I am doing! The error_log output is showing the variable name including the curly brackets. $message = preg_replace_callback( "/\{\{([a-zA

Converting preg_replace to preg_replace_callback for finding and replacing words with variables

只愿长相守 提交于 2020-04-07 09:14:42
问题 I have the following line of code: $message = preg_replace('/\{\{([a-zA-Z_-]+)\}\}/e', "$$1", $body); This replaces words surrounded by two curly brackets with variables of the same name. ie {{username}} gets replaced by $username. I am trying to convert it to use preg_replace_callback. This is my code so far based on Googling, but I'm not really sure what I am doing! The error_log output is showing the variable name including the curly brackets. $message = preg_replace_callback( "/\{\{([a-zA

Converting preg_replace to preg_replace_callback for finding and replacing words with variables

不问归期 提交于 2020-04-07 09:14:12
问题 I have the following line of code: $message = preg_replace('/\{\{([a-zA-Z_-]+)\}\}/e', "$$1", $body); This replaces words surrounded by two curly brackets with variables of the same name. ie {{username}} gets replaced by $username. I am trying to convert it to use preg_replace_callback. This is my code so far based on Googling, but I'm not really sure what I am doing! The error_log output is showing the variable name including the curly brackets. $message = preg_replace_callback( "/\{\{([a-zA

Converting preg_replace to preg_replace_callback for finding and replacing words with variables

[亡魂溺海] 提交于 2020-04-07 09:14:10
问题 I have the following line of code: $message = preg_replace('/\{\{([a-zA-Z_-]+)\}\}/e', "$$1", $body); This replaces words surrounded by two curly brackets with variables of the same name. ie {{username}} gets replaced by $username. I am trying to convert it to use preg_replace_callback. This is my code so far based on Googling, but I'm not really sure what I am doing! The error_log output is showing the variable name including the curly brackets. $message = preg_replace_callback( "/\{\{([a-zA

How do I access a variable inside of preg_replace_callback?

瘦欲@ 提交于 2020-01-13 07:50:07
问题 I'm trying to replace {{key}} items in my $text with values from a passed array. but when I tried adding the print_r to see what was going on I got a Undefined variable: kvPairs error. How can I access my variable form within the preg_replace_callback ? public function replaceValues($kvPairs, $text) { $text = preg_replace_callback( '/(\{{)(.*?)(\}})/', function ($match) { $attr = trim($match[2]); print_r($kvPairs[strtolower($attr)]); if (isset($kvPairs[strtolower($attr)])) { return "<span

preg_replace_callback - do twice

冷暖自知 提交于 2019-12-31 05:35:09
问题 Yo, i'm trying to do this script working, but it doesn't work. How do i do it twice, the preg_replace_callback with two different functions. Thanks! function prepend_proxy($matches) { $url = (substr($_GET['url'], 0, 7) == 'http://') ? $_GET['url'] : "http://{$_GET['url']}"; $prepend = $matches[2] ? $matches[2] : $url; $prepend = 'proxy2.php?url='. $prepend .'/'; return $matches[1] . $prepend . $matches[3]; } function imgprepend_proxy($matches2) { $url = (substr($_GET['url'], 0, 7) == 'http://