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

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

WBOY
Release: 2016-06-13 12:04:11
Original
977 people have browsed it

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 />}
Copy after login


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

>

>

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


想要的结果

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



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

------解决方案--------------------
$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 />}
Copy after login

------解决方案--------------------
$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 />}
Copy after login

Related labels:
source:php.cn
Previous article:一种实用的处理表单参数和URL参数的方法,你想到了吗 Next article:数据库读出来的数据如何与定义好的常量对应
Statement of this Website
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
Latest Articles by Author
Latest Issues
Related Topics
More>
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template