Home > Backend Development > PHP Tutorial > 一个字符串难题~

一个字符串难题~

WBOY
Release: 2016-06-23 14:02:06
Original
917 people have browsed it

<?php$a="<fieldset>1<fieldset>2<fieldset>3<fieldset>4</fieldset></fieldset></fieldset></fieldset>";echo $a;?>
Copy after login


最终结果要变成

$a="<fieldset>1<fieldset>2<fieldset>4</fieldset></fieldset></fieldset>";
Copy after login


把第3个去掉~~~

怎么做的?


回复讨论(解决方案)

应该是用js做的或者你利用php的字符串函数也能

可用正则平衡组做

explode
str_replace
preg_replace


说得好像很轻松呀

说得好像很轻松呀
str_replace就可以吧 写个函数传个参数进去,你上面例子就传3 然后前找出< 和>之前的内容 一些替换为空
再去掉最后一个之前的内容

$a="<fieldset>1<fieldset>2<fieldset>3<fieldset>4</fieldset></fieldset></fieldset></fieldset>";$aa=preg_replace("/((<fieldset>[^<]+){2})(<fieldset>[^<]+((<fieldset>[^<]+<\/fieldset>)*)<\/fieldset>)((<\/fieldset>){2})/isU", "\\1\\4\\6", $a);echo $aa;
Copy after login


是不是这样
{}里的数字可以改,想这里写2是因为第三个

前有两个

确实不轻松  楼主给分哈
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template