php
  • regex
  • 2011-02-14 94 views 0 likes 
    0

    腦今天不工作 - 任何人都可以給我一個正則表達式會變成是:刪除左右雙引號 「功能(){...}」

    {events:{click:"function() { alert('hi'); }"}}}}} 
    

    到:

    {events:{click:function() { alert('hi'); }}}}}} 
    

    任何任何其他實例如字符串中的這個。

    如果這有助於擴大我的問題:

    到目前爲止,使用這樣的:

    $replacement = '${1}:'; 
    $json_options = preg_replace('/"(\w+)"\s*:/', $replacement, $json_options); 
    

    我把它變成這樣:

    string(1051) "{"chart":{"renderTo":"tx_count","defaultSeriesType":"spline"},"credits":{"enabled":false},"series":[{"type":"spline","name":"Transactions Per Day","color":"#4572A7","data":[3,5,3,3,3,6,6,92,2]},{"type":"spline","name":"Value Per Day","yAxis":1,"color":"#89A54E","data":[30,232,172.99,30,160,550,596,2407.96,20]},{"type":"spline","name":"Average Value Per Day","yAxis":2,"color":"#AA4643","data":[10,46.4,57.7,10,53.3,91.7,99.3,26.2,10]}],"legend":{"enabled":true},"xAxis":{"labels":{"rotation":"-45"},"categories":["02\/02\/2011","03\/02\/2011","06\/02\/2011","07\/02\/2011","08\/02\/2011","09\/02\/2011","10\/02\/2011","11\/02\/2011","14\/02\/2011"]},"title":{"text":"Transactions Summary","align":"center","x":0,"y":20},"yAxis":[{"title":{"text":"Transactions","style":{"color":"#4572A7"}}},{"title":{"text":"Value","style":{"color":"#89A54E"}},"opposite":true},{"title":{"text":"Value (Average)","style":{"color":"#AA4643"}},"opposite":true}],"plotOptions":{"series":{"cursor":"pointer","point":{"events":{"click":"function() { alert('hi'); }"}}}}}" 
    

    到這個(這是完美的),

    string(947) "{chart:{renderTo:"tx_count",defaultSeriesType:"spline"},credits:{enabled:false},series:[{type:"spline",name:"Transactions Per Day",color:"#4572A7",data:[3,5,3,3,3,6,6,92,2]},{type:"spline",name:"Value Per Day",yAxis:1,color:"#89A54E",data:[30,232,172.99,30,160,550,596,2407.96,20]},{type:"spline",name:"Average Value Per Day",yAxis:2,color:"#AA4643",data:[10,46.4,57.7,10,53.3,91.7,99.3,26.2,10]}],legend:{enabled:true},xAxis:{labels:{rotation:"-45"},categories:["02\/02\/2011","03\/02\/2011","06\/02\/2011","07\/02\/2011","08\/02\/2011","09\/02\/2011","10\/02\/2011","11\/02\/2011","14\/02\/2011"]},title:{text:"Transactions Summary",align:"center",x:0,y:20},yAxis:[{title:{text:"Transactions",style:{color:"#4572A7"}}},{title:{text:"Value",style:{color:"#89A54E"}},opposite:true},{title:{text:"Value (Average)",style:{color:"#AA4643"}},opposite:true}],plotOptions:{series:{cursor:"pointer",point:{events:{click:"function() { alert('hi'); }"}}}}}" 
    

    now i ne編輯刪除可能在字符串中的任何函數(){... stuff ...}的雙引號。

    +0

    你只是想刪除所有引號? – Tim 2011-02-14 13:02:44

    +0

    nope只是圍繞any:thing:「function(){.some other code ...}」 – Brian 2011-02-14 13:04:18

    +0

    你想刪除字符串中的第一個和最後一個引號?引號是如何進入該字符串的?你能阻止他們進入那裏嗎?如果下面Briedis的解決方案沒有解決您的問題,請更新以澄清。 – Gordon 2011-02-14 13:08:07

    回答

    0

    這個工作對我來說:

    <?php preg_replace('/"(function\s*?\(\)\s*?{.*?})"/', "$1", $string); ?> 
    
    5

    你可以$string = str_replace('"', "", $string);,在這個特定的例子中不需要正則表達式。

    或者試試這則:

    $string = str_replace(array('"function()','}"}'), array('function()', '}}'), $string); 
    
    相關問題