Home > Backend Development > PHP Tutorial > php 正规替换的结果怎么位移

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

WBOY
Release: 2016-06-23 13:52:02
Original
719 people have browsed it


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


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

>

>

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


想要的结果

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



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


回复讨论(解决方案)

没有看出两个结果的区别!

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

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





追问一下:如果我的html是这个形式,应该怎么改下

$str='

';

想要的结果

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









$str='<div _data="111" class="aaa"></div><div _data="222" class="bbb"></div><p _data="333" class="ccc"></p>';$a=preg_replace_callback('/<(\w+)\s+\_data=[\"|\']([a-zA-Z0-9\_]+)([\s\"\']*)\s*class="(.+?)"><\/\1>/is', "data_array", $str);die($a);  function data_array($key){    $code=add_div($key[4], $key[2]);    return $code;   }  function add_div($class, $id){         $value="<div class=\"$class\"><div id=\"$id\"></div></div>";      return $value;   }
Copy after login

Related labels:
source:php.cn
Previous article:PHP从MySQL读取数据输出乱码 Next article:对一个php foreach的疑问,请高手指点
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