Home  >  Article  >  Backend Development  >  关于preg_replace_callback的用法,该如何解决

关于preg_replace_callback的用法,该如何解决

WBOY
WBOYOriginal
2016-06-13 12:07:191110browse

关于preg_replace_callback的用法

不知道为什么这个user获取到,在其他的地方是可以获取到的,但是在这个回调函数中获取不到,忘高手指点,其他的地方都是没问题的,我测试了很多次,username那里,就是那样的写的,因为它是个字符串
------解决思路----------------------
这个地方涉及PHP的闭包  要在匿名函数中加上use($user)     关于闭包详细资料请百度之

function replace_var($user,$str){<br />	<br />	$varPattern = '/\{\$.*?\}/';<br />	<br />	$str = preg_replace_callback($varPattern,function($matches) use($user){<br />		if($matches[0] == '{$usrename}'){<br />			var_dump($user);<br />		}<br />	},$str);<br /><br />}

------解决思路----------------------
这与闭包没有关系!
是变量的作用域问题

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn