作者tnsshnews (tnsshnews)
看板PHP
標題[請益]請問怎樣string replace
時間Wed Oct 5 18:11:48 2011
請問要將下面兩行
<wf cmd=ignore pos=IN>of</wf>
<wf cmd=done pos=VBZ ot=notag>is</wf>
取代成
<wf cmd="ignore" pos="IN">of</wf>
<wf cmd="done" pos="VBZ" ot="notag">is</wf>
似乎只用str_replace無法完成, 而且有時=後面的字後面會接空格,
有時候又會是>, 懇請高手賜教!!!
感激不盡
謝謝
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.109.18.168
1F:→ mervynW:preg_replace('=\ ?') 10/05 18:24
2F:→ tyf99:regex 10/05 18:25
3F:→ tyf99:prematch & postmatch 10/05 18:31
4F:→ tnsshnews:請問要怎樣取代呢? 一取代就把中間的屬性質都變空了 10/05 22:25
5F:推 mervynW:preg_replace('/(\w+)=\ ?(\w+)/', '\\1="\\2"') 10/05 22:39
6F:→ tnsshnews:謝謝樓上, 好像是取出of與is,沒有取代成""? 10/05 23:13
7F:→ tnsshnews:不知道是不是我搞錯了, regex真是難懂的東西 10/05 23:14
8F:→ tnsshnews:$string="<wf cmd=ignore pos=IN>for</wf>" 10/05 23:15
9F:→ tnsshnews:preg_replace('/(\w+)=\ ?(\w+)/', '\\1="\\2"',$string 10/05 23:16