Home > Backend Development > PHP Tutorial > 求正则 $aaa=bbb 变 aaa="bbb"

求正则 $aaa=bbb 变 aaa="bbb"

WBOY
Release: 2016-06-23 13:28:35
Original
964 people have browsed it

1 比如 $table=Channel
变成 $table=Channel

2 比如 $Row=10
变成 $Row=10

3 比如  $table=Channel 
变成  $table=Channel 

结构说明
原结构必须是  结尾(>的左边 可能存在空格 也可能不存在)    非此结构内的 部分 不会变化
求正则

自己写了一个 但不完美
$p = '/\$(\w+)=(\w+)/i' ; 
$r = '$1="$2"' ;
$c = preg_replace($p, $r, $c); 

无法匹配 Where=[FatherID]=0 部分 也无法判断是否在 内部 

能匹配上方三个例子的 给全部分


回复讨论(解决方案)

$Sql=select * from [{pre}Content] where [cid] in (3,4,5)
这个没有分界符号,不能判断,最后加个分号吧

<?php$s = '<qlist:m listname="m"  $Row=10 $table=Channel $Where=[FatherID]=0 >$table=Channel<qlist:aa listname="aa"  $Row=10 $Sql=select * from [{pre}Content] where [cid] in (3,4,5); $Type=Images>$Row=10<qlist:bb listname="bb"  $Row=10> $table=Channel ';echo preg_replace(  array('/(<.*)\$(Row=)(\d+)(.*>)/', '/(<.*)\$(table=)(\w+)(.*>)/','/(<.*)\$(Where=)(\[\w+\]=)(\d+)(.*>)/','/(<.*)\$(Type=)(\w+)(.*>)/', '/(<.*)\$(Sql=)(.*;)(.*>)/'),  array('$1$2"$3"$4', '$1$2"$3"$4', '$1$2"$3$4"$5', '$1$2"$3"$4', '$1$2"$3"$4'),  $s);?>
Copy after login
Copy after login



$table=Channel
$Row=10
$table=Channel

$Sql=select * from [{pre}Content] where [cid] in (3,4,5)
这个没有分界符号,不能判断,最后加个分号吧

<?php$s = '<qlist:m listname="m"  $Row=10 $table=Channel $Where=[FatherID]=0 >$table=Channel<qlist:aa listname="aa"  $Row=10 $Sql=select * from [{pre}Content] where [cid] in (3,4,5); $Type=Images>$Row=10<qlist:bb listname="bb"  $Row=10> $table=Channel ';echo preg_replace(  array('/(<.*)\$(Row=)(\d+)(.*>)/', '/(<.*)\$(table=)(\w+)(.*>)/','/(<.*)\$(Where=)(\[\w+\]=)(\d+)(.*>)/','/(<.*)\$(Type=)(\w+)(.*>)/', '/(<.*)\$(Sql=)(.*;)(.*>)/'),  array('$1$2"$3"$4', '$1$2"$3"$4', '$1$2"$3$4"$5', '$1$2"$3"$4', '$1$2"$3"$4'),  $s);?>
Copy after login
Copy after login



$table=Channel
$Row=10
 $table=Channel 






我有个思路哈 看您是否能改进 分号不行呢

可以利用 $aa=bb $ 来分割
先把 中间的部分提出来
在结尾加个  $ 然后再分割
$Row=10 $table=Channel $Where=[FatherID]=0 $
这样子 您看能否改进下 谢谢

你要保证最后有个$才行,思路你都想出了,用我的改改也不难。
加油。

其实是$sql那个因为没有结尾符号判断,所以才需要加分号,其他其实已经实现功能了。

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