preg-replace-callback

php regex preg_replace_callback fails to handle mutliple lines

落花浮王杯 提交于 2019-12-11 19:16:42
问题 I have a code like $newstr = preg_replace_callback("/<p>(.+?)</p>/", function($matches) { return str_replace($matches[1], '<b>' . $matches[1] . '</b>', $matches[0]); }, $str); It replaces if a single line string is given like this '<p>Hello World and Hello Universe</p>' but fails when multiple lines are given like '<p>Hello World and Hello Universe</p>' How it can be handled? If for test purpose I give string like this '<p>Hello World and'. 'Hello Universe</p>' It works but the problem is

preg_replace_callback pattern issue

烂漫一生 提交于 2019-12-11 16:45:00
问题 I'm using the following pattern to capture links, and turn them into HTML friendly links. I use the following pattern in a preg_replace_callback and for the most part it works. "#(https?|ftp)://(\S+[^\s.,>)\];'\"!?])#" But this pattern fails when the text reads like so: http://mylink.com/page[/b] At that point it captures the [/b amusing it is part of the link, resulting in this: <a href="http://woodmill.co.uk[/b">woodmill.co.uk[/b</a>] I've look over the pattern, and used some cheat sheets

Can I somehow know which replacement is taking place from within a callback of preg_replace_callback?

北慕城南 提交于 2019-12-11 12:52:07
问题 I'm using preg_replace_callback to substitute particular tokens within the string. But apart from actual token I need to know as well whether that token was first, second or third in a subject string. Is there any way to access that info? I found an argument $count in preg_replace_callback definition (http://php.net/manual/en/function.preg-replace-callback.php), which counts replacements, but I'm not sure if it is accessible from within callback. Any example of the usage in described context?

PHP Matching URLs and using preg_replace_callback()

心不动则不痛 提交于 2019-12-11 08:27:50
问题 I use the following to find all URL´s inside $content $content = preg_match_all( '/(http[s]?:[^\s]*)/i', $content, $links ); But this will depend on the http:// part in http://www.google.com/some/path . My questions are : 1 - How can I modify it in order to hit also the links that are start with only www , e.g. www.google.com ? 2 - The main aim is to find the links, and replace them with a value that is returned from another function. I tried preg_match_callback() , but it is not working

PHP Regex Replace Image SRC Attribute

▼魔方 西西 提交于 2019-12-11 05:25:26
问题 I'm trying to find a regular expression that would allow me replace the SRC attribute in an image. Here is what I have: function getURL($matches) { global $rootURL; return $rootURL . "?type=image&URL=" . base64_encode($matches['1']); } $contents = preg_replace_callback("/<img[^>]*src *= *[\"']?([^\"']*)/i", getURL, $contents); For the most part, this works well, except that anything before the src=" attribute is eliminated when $contents is echoed to the screen. In the end, SRC is updated

FROM preg_replace TO preg_replace_callback

ⅰ亾dé卋堺 提交于 2019-12-11 03:05:21
问题 Hello fellow netheads! I'm having issues with updating some old function to preg_replace_callback. Edit: what am I doing wrong? This is my first (preg_replace/deprecated) function: if ($handle) { while (!feof($handle)) { $buffer = fgets($handle, 4096); @eval('$templ = new '.TEMPL_CLASS.';'); $buffer = preg_replace("#§([a-z0-9-_]+)\.?([a-z0-9-_]+)?#ie","\$templ->\\1(\\2)",$buffer); $out .= $buffer; } fclose($handle); } Second function (this is my attempt at converting to preg_replace_callback)

Use a local variable in preg_replace_callback - PHP

ⅰ亾dé卋堺 提交于 2019-12-08 08:18:12
问题 How to use a local variable in preg_replace_callback in PHP. I have the following code: function pregRep($matches) { global $i; $i++; if($i > 2) { return '#'.$matches[0]; } else { return $matches[0]; } } $i = 0; $str = preg_replace_callback($reg_exp,"pregRep",$str); And also $str is a string, $reg_exp is a regex expression. Both of these are well defined. Thanks for your help. 回答1: The easiest way is with an anonymous callback: $str = preg_replace_callback($regExp,function($match) use ($some

Custom parsing function PHP

◇◆丶佛笑我妖孽 提交于 2019-12-07 13:02:40
问题 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); }

Use a local variable in preg_replace_callback - PHP

冷暖自知 提交于 2019-12-06 16:59:33
How to use a local variable in preg_replace_callback in PHP. I have the following code: function pregRep($matches) { global $i; $i++; if($i > 2) { return '#'.$matches[0]; } else { return $matches[0]; } } $i = 0; $str = preg_replace_callback($reg_exp,"pregRep",$str); And also $str is a string, $reg_exp is a regex expression. Both of these are well defined. Thanks for your help. The easiest way is with an anonymous callback: $str = preg_replace_callback($regExp,function($match) use ($some_local_variable) { // do something },$str); Note that you can add multiple variables in this way, but it will

Second parameter in preg_replace_callback()

时光毁灭记忆、已成空白 提交于 2019-12-06 14:52:46
问题 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! 回答1: The callback is called as