将引号添加到字符串中的数字

时间:2012-06-27 11:11:28

标签: php regex replace

我有以下几乎 JSON字符串:

<?php
$string = '{0:{"id":1,"name":"Antiek en Kunst","subs":[{"id":2,"name":"Antiek | Bestek"},{"id":3,"name":"Antiek | Boeken en Bijbels"},{"id":1100,"name":"Antiek | Emaille"},{"id":1501,"name":"Antiek | Gereedschap en Instrumenten"},{"id":1648,"name":"Antiek | Glas en Kristal"},{"id":2614,"name":"Antiek | Goud en Zilver"},{"id":2661,"name":"Antiek | Kandelaars"},{"id":1841,"name":"Antiek | Kantoor en Zakelijk"},{"id":1502,"name":"Antiek | Keramiek en Aardewerk"},{"id":1842,"name":"Antiek | Keukenbenodigdheden"},{"id":2118,"name":"Antiek | Kleden en Textiel"},{"id":1503,"name":"Antiek | Kleding en Accessoires"},{"id":6,"name":"Antiek | Klokken"},{"id":1647,"name":"Antiek | Koper en Brons"},{"id":7,"name":"Antiek | Lampen"},{"id":1504,"name":"Antiek | Meubels | Bedden"},{"id":5,"name":"Antiek | Meubels | Kasten"},{"id":1505,"name":"Antiek | Meubels | Stoelen en Banken"},{"id":1506,"name":"Antiek | Meubels | Tafels"},{"id":1101,"name":"Antiek | Naaimachines"},{"id":10,"name":"Antiek | Porselein"},{"id":1102,"name":"Antiek | Religie"},{"id":1103,"name":"Antiek | Schalen"},{"id":2662,"name":"Antiek | Schoolplaten"},{"id":1843,"name":"Antiek | Servies compleet"},{"id":12,"name":"Antiek | Servies los"},{"id":1507,"name":"Antiek | Speelgoed"},{"id":2663,"name":"Antiek | Spiegels"},{"id":2664,"name":"Antiek | Tin"},{"id":11,"name":"Antiek | Tv's en Audio"},{"id":14,"name":"Antiek | Vazen"},{"id":1104,"name":"Antiek | Wandborden en Tegels"},{"id":1500,"name":"Antiek | Woonaccessoires"},{"id":9,"name":"Antiek | Overige"},{"id":15,"name":"Curiosa en Brocante"},{"id":1423,"name":"Diensten | Kunstenaars en Portretschilders"},{"id":2610,"name":"Diensten | Reparatie en Onderhoud"},{"id":23,"name":"Kunst | Beelden en Houtsnijwerken"},{"id":1508,"name":"Kunst | Designobjecten"},{"id":1105,"name":"Kunst | Etsen en Gravures"},{"id":27,"name":"Kunst | Litho's en Zeefdrukken"},{"id":1844,"name":"Kunst | Niet-Westerse kunst"},{"id":1846,"name":"Kunst | Schilderijen | Abstract"},{"id":25,"name":"Kunst | Schilderijen | Klassiek"},{"id":1845,"name":"Kunst | Schilderijen | Modern"},{"id":26,"name":"Kunst | Tekeningen en Foto's"},{"id":24,"name":"Kunst | Overige"}]},1:{"id":31,"name":"Audio, Tv en Foto","subs":[{"id":2035,"name":"Accu's en Batterijen"},{"id":2617,"name":"Afstandsbedieningen"},{"id":32,"name":"Bandrecorders"},{"id":1132,"name":"Beamers"},{"id":2665,"name":"Blu-ray-spelers en Blu-ray-recorders"},{"id":33,"name":"Buizenversterkers"},{"id":2036,"name":"Cassettedecks"},{"id":35,"name":"Cd-spelers"},{"id":2666,"name":"Diaprojectors"},{"id":2042,"name":"Diensten | Film- en Videobewerking"},{"id":1199,"name":"Diensten | Fotografen"},{"id":1219,"name":"Diensten | Reparaties"},{"id":1114,"name":"Dvd-spelers en Dvd-recorders"},{"id":1115,"name":"Films 8mm, 16mm en 35mm"},{"id":1360,"name":"Fotografie | Accu's en Batterijen"},{"id":480,"name":"Fotografie | Camera's | Analoog"},{"id":487,"name":"Fotografie | Camera's | Digitaal"},{"id":2667,"name":"Fotografie | Digitale fotolijstjes"},{"id":488,"name":"Fotografie | Doka Toebehoren"},{"id":1720,"name":"Fotografie | Filters"},{"id":489,"name":"Fotografie | Flitsers"},{"id":1483,"name":"Fotografie | Fotolijsten en Fotoalbums"},{"id":1721,"name":"Fotografie | Fotoprinters en Fotopapier"},{"id":1400,"name":"Fotografie | Fotostudio en Toebehoren"},{"id":1484,"name":"Fotografie | Fototassen"},{"id":493,"name":"Fotografie | Geheugenkaarten"},{"id":495,"name":"Fotografie | Lenzen en Objectieven"},{"id":497,"name":"Fotografie | Onderwatercamera's"},{"id":501,"name":"Fotografie | Professionele apparatuur"},{"id":500,"name":"Fotografie | Statieven en Balhoofden"},{"id":1722,"name":"Harddiskrecorders"},{"id":1116,"name":"Home Cinema-sets"},{"id":1106,"name":"Kabels"},{"id":37,"name":"Koptelefoons en Headsets"},{"id":38,"name":"Luidsprekers"},{"id":2668,"name":"Mediaspelers"},{"id":40,"name":"Mp3-spelers | Apple iPod"},{"id":1649,"name":"Mp3-spelers | Overige merken"},{"id":1723,"name":"Mp3-spelers | Accessoires | Apple iPod"},{"id":1452,"name":"Mp3-spelers | Accessoires | Overige merken"},{"id":2615,"name":"Mp4-spelers"},{"id":1724,"name":"Opladers"},{"id":496,"name":"Optische apparatuur | Microscopen"},{"id":502,"name":"Optische apparatuur | Telescopen"},{"id":503,"name":"Optische apparatuur | Verrekijkers"},{"id":42,"name":"Platenspelers en Pick-ups"},{"id":1117,"name":"Professionele Audio-, Tv- en Video-apparatuur"},{"id":43,"name":"Radio's"},{"id":36,"name":"Stereo-sets"},{"id":45,"name":"Tuners"},{"id":1118,"name":"Tv-decoders en Schotelantennes"},{"id":1120,"name":"Tv's | Plasma, Lcd en Led"},{"id":1121,"name":"Tv's | Overige Tv's"},{"id":1453,"name":"Tv's | Accessoires"},{"id":46,"name":"Versterkers en Receivers"},{"id":1129,"name":"Videobewakingsapparatuur"},{"id":1130,"name":"Videocamera's | Analoog"},{"id":1131,"name":"Videocamera's | Digitaal"},{"id":1133,"name":"Videospelers en Videorecorders"},{"id":47,"name":"Walkmans, Discmans en Minidiscspelers"},{"id":1725,"name":"Weerstations en Barometers"},{"id":41,"name":"Overige Audio, Tv en Foto"}]}}';

?>

我需要将所有整数转换为字符串,这可以通过向它们添加双引号(")来完成。但是,怎么做呢?它不需要特定于PHP,notepad ++能够使用正则表达式进行搜索和替换,但我不确定这是否是正确的方法。任何帮助表示赞赏。

3 个答案:

答案 0 :(得分:1)

$result = preg_replace('/(?<=[{:,])\d+(?=[:,])/', '"\\0"', $string); 

答案 1 :(得分:-1)

虽然将整数转换为字符串是不对的(在JSON编号中不应该在引号中),但从技术上讲,你要找的是:

$string = '{0:{"id":1,"name":"Antiek en Kunst","subs":[{"id":2,"name":"Antiek | Bestek"},{"id":3,"name":"Antiek | Boeken en Bijbels"},{"id":1100,"name":"Antiek | Emaille"},{"id":1501,"name":"Antiek | Gereedschap en Instrumenten"},{"id":1648,"name":"Antiek | Glas en Kristal"},{"id":2614,"name":"Antiek | Goud en Zilver"},{"id":2661,"name":"Antiek | Kandelaars"},{"id":1841,"name":"Antiek | Kantoor en Zakelijk"},{"id":1502,"name":"Antiek | Keramiek en Aardewerk"},{"id":1842,"name":"Antiek | Keukenbenodigdheden"},{"id":2118,"name":"Antiek | Kleden en Textiel"},{"id":1503,"name":"Antiek | Kleding en Accessoires"},{"id":6,"name":"Antiek | Klokken"},{"id":1647,"name":"Antiek | Koper en Brons"},{"id":7,"name":"Antiek | Lampen"},{"id":1504,"name":"Antiek | Meubels | Bedden"},{"id":5,"name":"Antiek | Meubels | Kasten"},{"id":1505,"name":"Antiek | Meubels | Stoelen en Banken"},{"id":1506,"name":"Antiek | Meubels | Tafels"},{"id":1101,"name":"Antiek | Naaimachines"},{"id":10,"name":"Antiek | Porselein"},{"id":1102,"name":"Antiek | Religie"},{"id":1103,"name":"Antiek | Schalen"},{"id":2662,"name":"Antiek | Schoolplaten"},{"id":1843,"name":"Antiek | Servies compleet"},{"id":12,"name":"Antiek | Servies los"},{"id":1507,"name":"Antiek | Speelgoed"},{"id":2663,"name":"Antiek | Spiegels"},{"id":2664,"name":"Antiek | Tin"},{"id":11,"name":"Antiek | Tv's en Audio"},{"id":14,"name":"Antiek | Vazen"},{"id":1104,"name":"Antiek | Wandborden en Tegels"},{"id":1500,"name":"Antiek | Woonaccessoires"},{"id":9,"name":"Antiek | Overige"},{"id":15,"name":"Curiosa en Brocante"},{"id":1423,"name":"Diensten | Kunstenaars en Portretschilders"},{"id":2610,"name":"Diensten | Reparatie en Onderhoud"},{"id":23,"name":"Kunst | Beelden en Houtsnijwerken"},{"id":1508,"name":"Kunst | Designobjecten"},{"id":1105,"name":"Kunst | Etsen en Gravures"},{"id":27,"name":"Kunst | Litho's en Zeefdrukken"},{"id":1844,"name":"Kunst | Niet-Westerse kunst"},{"id":1846,"name":"Kunst | Schilderijen | Abstract"},{"id":25,"name":"Kunst | Schilderijen | Klassiek"},{"id":1845,"name":"Kunst | Schilderijen | Modern"},{"id":26,"name":"Kunst | Tekeningen en Foto's"},{"id":24,"name":"Kunst | Overige"}]},1:{"id":31,"name":"Audio, Tv en Foto","subs":[{"id":2035,"name":"Accu's en Batterijen"},{"id":2617,"name":"Afstandsbedieningen"},{"id":32,"name":"Bandrecorders"},{"id":1132,"name":"Beamers"},{"id":2665,"name":"Blu-ray-spelers en Blu-ray-recorders"},{"id":33,"name":"Buizenversterkers"},{"id":2036,"name":"Cassettedecks"},{"id":35,"name":"Cd-spelers"},{"id":2666,"name":"Diaprojectors"},{"id":2042,"name":"Diensten | Film- en Videobewerking"},{"id":1199,"name":"Diensten | Fotografen"},{"id":1219,"name":"Diensten | Reparaties"},{"id":1114,"name":"Dvd-spelers en Dvd-recorders"},{"id":1115,"name":"Films 8mm, 16mm en 35mm"},{"id":1360,"name":"Fotografie | Accu's en Batterijen"},{"id":480,"name":"Fotografie | Camera's | Analoog"},{"id":487,"name":"Fotografie | Camera's | Digitaal"},{"id":2667,"name":"Fotografie | Digitale fotolijstjes"},{"id":488,"name":"Fotografie | Doka Toebehoren"},{"id":1720,"name":"Fotografie | Filters"},{"id":489,"name":"Fotografie | Flitsers"},{"id":1483,"name":"Fotografie | Fotolijsten en Fotoalbums"},{"id":1721,"name":"Fotografie | Fotoprinters en Fotopapier"},{"id":1400,"name":"Fotografie | Fotostudio en Toebehoren"},{"id":1484,"name":"Fotografie | Fototassen"},{"id":493,"name":"Fotografie | Geheugenkaarten"},{"id":495,"name":"Fotografie | Lenzen en Objectieven"},{"id":497,"name":"Fotografie | Onderwatercamera's"},{"id":501,"name":"Fotografie | Professionele apparatuur"},{"id":500,"name":"Fotografie | Statieven en Balhoofden"},{"id":1722,"name":"Harddiskrecorders"},{"id":1116,"name":"Home Cinema-sets"},{"id":1106,"name":"Kabels"},{"id":37,"name":"Koptelefoons en Headsets"},{"id":38,"name":"Luidsprekers"},{"id":2668,"name":"Mediaspelers"},{"id":40,"name":"Mp3-spelers | Apple iPod"},{"id":1649,"name":"Mp3-spelers | Overige merken"},{"id":1723,"name":"Mp3-spelers | Accessoires | Apple iPod"},{"id":1452,"name":"Mp3-spelers | Accessoires | Overige merken"},{"id":2615,"name":"Mp4-spelers"},{"id":1724,"name":"Opladers"},{"id":496,"name":"Optische apparatuur | Microscopen"},{"id":502,"name":"Optische apparatuur | Telescopen"},{"id":503,"name":"Optische apparatuur | Verrekijkers"},{"id":42,"name":"Platenspelers en Pick-ups"},{"id":1117,"name":"Professionele Audio-, Tv- en Video-apparatuur"},{"id":43,"name":"Radio's"},{"id":36,"name":"Stereo-sets"},{"id":45,"name":"Tuners"},{"id":1118,"name":"Tv-decoders en Schotelantennes"},{"id":1120,"name":"Tv's | Plasma, Lcd en Led"},{"id":1121,"name":"Tv's | Overige Tv's"},{"id":1453,"name":"Tv's | Accessoires"},{"id":46,"name":"Versterkers en Receivers"},{"id":1129,"name":"Videobewakingsapparatuur"},{"id":1130,"name":"Videocamera's | Analoog"},{"id":1131,"name":"Videocamera's | Digitaal"},{"id":1133,"name":"Videospelers en Videorecorders"},{"id":47,"name":"Walkmans, Discmans en Minidiscspelers"},{"id":1725,"name":"Weerstations en Barometers"},{"id":41,"name":"Overige Audio, Tv en Foto"}]}}';

$result = preg_replace('/\d+/', '"\\0"', $string);

编辑:如果您想将其转换为正确的JSON,那么

$string = '{0:{"id":1,"name":"Antiek en Kunst","subs":[{"id":2,"name":"Antiek | Bestek"},{"id":3,"name":"Antiek | Boeken en Bijbels"},{"id":1100,"name":"Antiek | Emaille"},{"id":1501,"name":"Antiek | Gereedschap en Instrumenten"},{"id":1648,"name":"Antiek | Glas en Kristal"},{"id":2614,"name":"Antiek | Goud en Zilver"},{"id":2661,"name":"Antiek | Kandelaars"},{"id":1841,"name":"Antiek | Kantoor en Zakelijk"},{"id":1502,"name":"Antiek | Keramiek en Aardewerk"},{"id":1842,"name":"Antiek | Keukenbenodigdheden"},{"id":2118,"name":"Antiek | Kleden en Textiel"},{"id":1503,"name":"Antiek | Kleding en Accessoires"},{"id":6,"name":"Antiek | Klokken"},{"id":1647,"name":"Antiek | Koper en Brons"},{"id":7,"name":"Antiek | Lampen"},{"id":1504,"name":"Antiek | Meubels | Bedden"},{"id":5,"name":"Antiek | Meubels | Kasten"},{"id":1505,"name":"Antiek | Meubels | Stoelen en Banken"},{"id":1506,"name":"Antiek | Meubels | Tafels"},{"id":1101,"name":"Antiek | Naaimachines"},{"id":10,"name":"Antiek | Porselein"},{"id":1102,"name":"Antiek | Religie"},{"id":1103,"name":"Antiek | Schalen"},{"id":2662,"name":"Antiek | Schoolplaten"},{"id":1843,"name":"Antiek | Servies compleet"},{"id":12,"name":"Antiek | Servies los"},{"id":1507,"name":"Antiek | Speelgoed"},{"id":2663,"name":"Antiek | Spiegels"},{"id":2664,"name":"Antiek | Tin"},{"id":11,"name":"Antiek | Tv's en Audio"},{"id":14,"name":"Antiek | Vazen"},{"id":1104,"name":"Antiek | Wandborden en Tegels"},{"id":1500,"name":"Antiek | Woonaccessoires"},{"id":9,"name":"Antiek | Overige"},{"id":15,"name":"Curiosa en Brocante"},{"id":1423,"name":"Diensten | Kunstenaars en Portretschilders"},{"id":2610,"name":"Diensten | Reparatie en Onderhoud"},{"id":23,"name":"Kunst | Beelden en Houtsnijwerken"},{"id":1508,"name":"Kunst | Designobjecten"},{"id":1105,"name":"Kunst | Etsen en Gravures"},{"id":27,"name":"Kunst | Litho's en Zeefdrukken"},{"id":1844,"name":"Kunst | Niet-Westerse kunst"},{"id":1846,"name":"Kunst | Schilderijen | Abstract"},{"id":25,"name":"Kunst | Schilderijen | Klassiek"},{"id":1845,"name":"Kunst | Schilderijen | Modern"},{"id":26,"name":"Kunst | Tekeningen en Foto's"},{"id":24,"name":"Kunst | Overige"}]},1:{"id":31,"name":"Audio, Tv en Foto","subs":[{"id":2035,"name":"Accu's en Batterijen"},{"id":2617,"name":"Afstandsbedieningen"},{"id":32,"name":"Bandrecorders"},{"id":1132,"name":"Beamers"},{"id":2665,"name":"Blu-ray-spelers en Blu-ray-recorders"},{"id":33,"name":"Buizenversterkers"},{"id":2036,"name":"Cassettedecks"},{"id":35,"name":"Cd-spelers"},{"id":2666,"name":"Diaprojectors"},{"id":2042,"name":"Diensten | Film- en Videobewerking"},{"id":1199,"name":"Diensten | Fotografen"},{"id":1219,"name":"Diensten | Reparaties"},{"id":1114,"name":"Dvd-spelers en Dvd-recorders"},{"id":1115,"name":"Films 8mm, 16mm en 35mm"},{"id":1360,"name":"Fotografie | Accu's en Batterijen"},{"id":480,"name":"Fotografie | Camera's | Analoog"},{"id":487,"name":"Fotografie | Camera's | Digitaal"},{"id":2667,"name":"Fotografie | Digitale fotolijstjes"},{"id":488,"name":"Fotografie | Doka Toebehoren"},{"id":1720,"name":"Fotografie | Filters"},{"id":489,"name":"Fotografie | Flitsers"},{"id":1483,"name":"Fotografie | Fotolijsten en Fotoalbums"},{"id":1721,"name":"Fotografie | Fotoprinters en Fotopapier"},{"id":1400,"name":"Fotografie | Fotostudio en Toebehoren"},{"id":1484,"name":"Fotografie | Fototassen"},{"id":493,"name":"Fotografie | Geheugenkaarten"},{"id":495,"name":"Fotografie | Lenzen en Objectieven"},{"id":497,"name":"Fotografie | Onderwatercamera's"},{"id":501,"name":"Fotografie | Professionele apparatuur"},{"id":500,"name":"Fotografie | Statieven en Balhoofden"},{"id":1722,"name":"Harddiskrecorders"},{"id":1116,"name":"Home Cinema-sets"},{"id":1106,"name":"Kabels"},{"id":37,"name":"Koptelefoons en Headsets"},{"id":38,"name":"Luidsprekers"},{"id":2668,"name":"Mediaspelers"},{"id":40,"name":"Mp3-spelers | Apple iPod"},{"id":1649,"name":"Mp3-spelers | Overige merken"},{"id":1723,"name":"Mp3-spelers | Accessoires | Apple iPod"},{"id":1452,"name":"Mp3-spelers | Accessoires | Overige merken"},{"id":2615,"name":"Mp4-spelers"},{"id":1724,"name":"Opladers"},{"id":496,"name":"Optische apparatuur | Microscopen"},{"id":502,"name":"Optische apparatuur | Telescopen"},{"id":503,"name":"Optische apparatuur | Verrekijkers"},{"id":42,"name":"Platenspelers en Pick-ups"},{"id":1117,"name":"Professionele Audio-, Tv- en Video-apparatuur"},{"id":43,"name":"Radio's"},{"id":36,"name":"Stereo-sets"},{"id":45,"name":"Tuners"},{"id":1118,"name":"Tv-decoders en Schotelantennes"},{"id":1120,"name":"Tv's | Plasma, Lcd en Led"},{"id":1121,"name":"Tv's | Overige Tv's"},{"id":1453,"name":"Tv's | Accessoires"},{"id":46,"name":"Versterkers en Receivers"},{"id":1129,"name":"Videobewakingsapparatuur"},{"id":1130,"name":"Videocamera's | Analoog"},{"id":1131,"name":"Videocamera's | Digitaal"},{"id":1133,"name":"Videospelers en Videorecorders"},{"id":47,"name":"Walkmans, Discmans en Minidiscspelers"},{"id":1725,"name":"Weerstations en Barometers"},{"id":41,"name":"Overige Audio, Tv en Foto"}]}}';

$result = preg_replace('/[\{\[]\s*(\d+)\s*:/', '"\\0"', $string);

这会将整数键转换为字符串,将整数值保留为整数。

答案 2 :(得分:-1)

这应该可以解决问题:

$json = preg_replace('/(\d+):/', '"$1":', $almost_json);