Home >Backend Development >PHP Tutorial >php 正规替换的结果如何位移?

php 正规替换的结果如何位移?

WBOY
WBOYOriginal
2016-06-13 12:04:11984browse

php 正规替换的结果怎么位移??

$str='<div _data="111"></div><div _data="222"></div><div _data="333"></div>';<br /><br />$a=preg_replace('/\_data=[\"|\']([a-zA-Z0-9\_]+)([\s\"\']*)/eis', "data_array('\\1')", $str);<br />die($a);<br /><br />function data_array($key){<br />	$code=add_div("$key");<br />	return "$code";	<br />}<br /><br />function add_div($v){	<br />	  $value="<div id=\"$v\"></div>";<br />	  return "$value";	<br />}


//得到的结果
***********************************************************************************************************

>
>
>
***********************************************************************************************************


想要的结果

***********************************************************************************************************
***********************************************************************************************************



***********************************************************************************************************
***********************************************************************************************************

------解决方案--------------------
$a=preg_replace('/\s+\_data=[\"<br><font color='#FF8000'>------解决方案--------------------</font><br>\']([a-zA-Z0-9\_]+)([\s\"\']*)>/eis', "data_array('\\1')", $str);<br />die($a);<br /> <br />function data_array($key){<br />    $code=add_div($key);<br />    return $code;   <br />}<br /> <br />function add_div($v){   <br />      $value="><div id=\"$v\"></div>";<br />      return $value;   <br />}

------解决方案--------------------
$str='<div _data="111" class="aaa"></div><div _data="222" class="bbb"></div><p _data="333" class="ccc"></p>';<br /><br />$a=preg_replace_callback('/<(\w+)\s+\_data=[\"<br><font color='#FF8000'>------解决方案--------------------</font><br>\']([a-zA-Z0-9\_]+)([\s\"\']*)\s*class="(.+?)"><\/\1>/is', "data_array", $str);<br />die($a);<br />  <br />function data_array($key){<br />    $code=add_div($key[4], $key[2]);<br />    return $code;   <br />}<br />  <br />function add_div($class, $id){   <br />      $value="<div class=\"$class\"><div id=\"$id\"></div></div>";<br />      return $value;   <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