【问题标题】:Get specific data from csv file从 csv 文件中获取特定数据
【发布时间】:2014-08-06 22:25:54
【问题描述】:

我有一个 csv 文件,我只想检索以下字段:

mobile=0786066486&phone_num=&mob_phone_num=6066486

举个例子,我的文件是什么样子的:

LeadID,ClientID,res,Name,Phone,Email,Serial,Time,Comment,Address,wasSent,Promocode,custDetails,DynamicSerialParams
7276199,B6FBEEBF-FE33-42D3-B0B8-83BF44F3C192,NULL,محمد ابوالرب,-786065524,king_moneer45@yahoo.com,339863,30/06/2014 00:00,<additional><pageName type='text'>masterLP - overnight96NDH5</pageName><FName type='text'>محمد</FName><LName type='text'>ابوالرب</LName><Mobile type='text'>0786066486</Mobile><Country type='text'>Jordan</Country><SendEmails>true</SendEmails><IP>91.186.228.92</IP><CountryID>107</CountryID><Language>Arabic</Language><source>zomztest</source></additional>,Jordan,1,b3b9ffe9-9301-4bd2-932f-cfab2f0f61d1,<OperatingSystem>Windows NT 6.1</OperatingSystem><UserLang>ar</UserLang><Browser>Chrome</Browser><BrowserVersion>Chrome 35.0.1916.153</BrowserVersion><Refferer>http://www.zomztest.ae/landing_pages/br1/ar/overnight96NDH5/?content=overnight96NDH5&dsp9=uae-t.com&dsp8=البورصة&dsp1=&SID=339863&gclid=CNmkp5KloL8CFcbKtAodk3gAag</Refferer><Url>http://l.zomztest.com/lead.ashx?fname=محمد&lname=ابوالرب&country=JO&email=king_moneer45@yahoo.com&phone=0786066486&sendmails=true&mobile=0786066486&phone_num=&mob_phone_num=6066486&area_code=&mob_area_code=078&cat=masterLP&promocode=&SID=339863&clang=AE&content=overnight96NDH5&gclid=CNmkp5KloL8CFcbKtAodk3gAag&dsp8=%D8%A7%D9%84%D8%A8%D9%88%D8%B1%D8%B5%D8%A9&dsp9=uae-t.com</Url><IP>91.186.228.92</IP>,dsp1=&dsp2=&dsp3=&dsp4=&dsp5=&dsp6=&dsp7=&dsp8=البورصة&dsp9=uae-t.com&dsp10=&dsp11=&dsp12=&dsp13=&dsp14=&dsp15=
7276199,B6FBEEBF-FE33-42D3-B0B8-83BF44F3C192,NULL,محمد ابوالرب,-786065524,king_moneer45@yahoo.com,339863,30/06/2014 00:00,<additional><pageName type='text'>masterLP - overnight96NDH5</pageName><FName type='text'>محمد</FName><LName type='text'>ابوالرب</LName><Mobile type='text'>0786066486</Mobile><Country type='text'>Jordan</Country><SendEmails>true</SendEmails><IP>91.186.228.92</IP><CountryID>107</CountryID><Language>Arabic</Language><source>zomztest</source></additional>,Jordan,1,b3b9ffe9-9301-4bd2-932f-cfab2f0f61d1,<OperatingSystem>Windows NT 6.1</OperatingSystem><UserLang>ar</UserLang><Browser>Chrome</Browser><BrowserVersion>Chrome 35.0.1916.153</BrowserVersion><Refferer>http://www.zomztest.ae/landing_pages/br1/ar/overnight96NDH5/?content=overnight96NDH5&dsp9=uae-t.com&dsp8=البورصة&dsp1=&SID=339863&gclid=CNmkp5KloL8CFcbKtAodk3gAag</Refferer><Url>http://l.zomztest.com/lead.ashx?fname=محمد&lname=ابوالرب&country=JO&email=king_moneer45@yahoo.com&phone=0786066486&sendmails=true&mobile=0786066486&phone_num=&mob_phone_num=6066486&area_code=&mob_area_code=078&cat=masterLP&promocode=&SID=339863&clang=AE&content=overnight96NDH5&gclid=CNmkp5KloL8CFcbKtAodk3gAag&dsp8=%D8%A7%D9%84%D8%A8%D9%88%D8%B1%D8%B5%D8%A9&dsp9=uae-t.com</Url><IP>91.186.228.92</IP>,dsp1=&dsp2=&dsp3=&dsp4=&dsp5=&dsp6=&dsp7=&dsp8=البورصة&dsp9=uae-t.com&dsp10=&dsp11=&dsp12=&dsp13=&dsp14=&dsp15=
7276200,B6FBEEBF-FE33-42D3-B0B8-83BF44F3C192,NULL,jose miguel abad tirado,-73501424,marjos091111@hotmail.com,236455,30/06/2014 00:01,<additional><pageName type='text'>masterLP - Testimonials5NoTime</pageName><FName type='text'>jose miguel</FName><LName type='text'>abad tirado</LName><Mobile type='text'>073943026717</Mobile><Country type='text'>Peru</Country><SA>yes</SA><SendEmails>true</SendEmails><IP>190.237.62.93</IP><CountryID>166</CountryID><Language>Spanish</Language><source>zomztest</source></additional>,Peru,1,598fa0fa-b033-964e-e204-eed2f6188f9a,<OperatingSystem>Windows NT 6.2; WOW64</OperatingSystem><UserLang>es-419</UserLang><Browser>Chrome</Browser><BrowserVersion>Chrome 35.0.1916.153</BrowserVersion><Refferer>http://www.zomztest.es/emerp/landing/masterlp/nobanslp.aspx?content=Testimonials5NoTime&mailT=630&delay=1&SA=Yes&dsp9=www.empresores.com&dsp1=&SID=236455&gclid=CKT-xcmloL8CFc9j7AodmRoAOQ</Refferer><Url>http://l.zomztest.com/lead.ashx?fname=jose+miguel&lname=abad+tirado&country=PE&email=marjos091111@hotmail.com&phone=073501475&sendmails=true&mobile=073943026717&phone_num=501475&mob_phone_num=943026717&area_code=073&mob_area_code=073&cat=masterLP&promocode=598fa0fa-b033-964e-e204-eed2f6188f9a&SID=236455&clang=ES&content=Testimonials5NoTime&mailT=630&delay=1&SA=Yes&gclid=CKT-xcmloL8CFc9j7AodmRoAOQ&dsp1=&dsp2=&dsp3=&dsp4=&dsp5=&dsp6=&dsp7=&dsp8=&dsp9=www.empresores.com&dsp10=&dsp11=google&dsp12=adsense&dsp13=/foros/26083-importar-de-poco-chile-bolivia-paraguay.html&dsp14=&dsp15=www.empresores.com</Url><IP>190.237.62.93</IP>,dsp1=&dsp2=&dsp3=&dsp4=&dsp5=&dsp6=&dsp7=&dsp8=&dsp9=www.empresores.com&dsp10=&dsp11=google&dsp12=adsense&dsp13=/foros/26083-importar-de-poco-chile-bolivia-paraguay.html&dsp14=&dsp15=www.empresores.com
7276200,B6FBEEBF-FE33-42D3-B0B8-83BF44F3C192,NULL,jose miguel abad tirado,-73501424,marjos091111@hotmail.com,236455,30/06/2014 00:01,<additional><pageName type='text'>masterLP - Testimonials5NoTime</pageName><FName type='text'>jose miguel</FName><LName type='text'>abad tirado</LName><Mobile type='text'>073943026717</Mobile><Country type='text'>Peru</Country><SA>yes</SA><SendEmails>true</SendEmails><IP>190.237.62.93</IP><CountryID>166</CountryID><Language>Spanish</Language><source>zomztest</source></additional>,Peru,1,598fa0fa-b033-964e-e204-eed2f6188f9a,<OperatingSystem>Windows NT 6.2; WOW64</OperatingSystem><UserLang>es-419</UserLang><Browser>Chrome</Browser><BrowserVersion>Chrome 35.0.1916.153</BrowserVersion><Refferer>http://www.zomztest.es/emerp/landing/masterlp/nobanslp.aspx?content=Testimonials5NoTime&mailT=630&delay=1&SA=Yes&dsp9=www.empresores.com&dsp1=&SID=236455&gclid=CKT-xcmloL8CFc9j7AodmRoAOQ</Refferer><Url>http://l.zomztest.com/lead.ashx?fname=jose+miguel&lname=abad+tirado&country=PE&email=marjos091111@hotmail.com&phone=073501475&sendmails=true&mobile=073943026717&phone_num=501475&mob_phone_num=943026717&area_code=073&mob_area_code=073&cat=masterLP&promocode=598fa0fa-b033-964e-e204-eed2f6188f9a&SID=236455&clang=ES&content=Testimonials5NoTime&mailT=630&delay=1&SA=Yes&gclid=CKT-xcmloL8CFc9j7AodmRoAOQ&dsp1=&dsp2=&dsp3=&dsp4=&dsp5=&dsp6=&dsp7=&dsp8=&dsp9=www.empresores.com&dsp10=&dsp11=google&dsp12=adsense&dsp13=/foros/26083-importar-de-poco-chile-bolivia-paraguay.html&dsp14=&dsp15=www.empresores.com</Url><IP>190.237.62.93</IP>,dsp1=&dsp2=&dsp3=&dsp4=&dsp5=&dsp6=&dsp7=&dsp8=&dsp9=www.empresores.com&dsp10=&dsp11=google&dsp12=adsense&dsp13=/foros/26083-importar-de-poco-chile-bolivia-paraguay.html&dsp14=&dsp15=www.empresores.com

我应该使用哪种正则表达式?

【问题讨论】:

  • 我相信这些人会感谢您在本网站上以纯文本形式发布他们的信息。
  • 最好使用 fgetcsv() 并简单地根据数组中的条目测试要检查的字段,而不是尝试使用正则表达式

标签: php regex csv


【解决方案1】:

您可以使用以下正则表达式:

mobile=(\d*)&phone_num=(\d*)&mob_phone_num=(\d*)

Working demo

MATCH 1
1.  [1113-1123] `0786066486`
2.  [1134-1134] ``
3.  [1149-1156] `6066486`
MATCH 2
1.  [2488-2498] `0786066486`
2.  [2509-2509] ``
3.  [2524-2531] `6066486`
MATCH 3
1.  [3946-3958] `073943026717`
2.  [3969-3975] `501475`
3.  [3990-3999] `943026717`
MATCH 4
1.  [5711-5723] `073943026717`
2.  [5734-5740] `501475`
3.  [5755-5764] `943026717`

【讨论】:

  • 你可以给我发邮件 karts22@gmx.com 我付钱给你写脚本吗?
  • ^^^ 很长一段时间以来我在 SO 上看到的最有趣的评论!! @Robi SO 不是代码编写服务,先学习正则表达式。尝试尝试不同的方法来尝试解决您的问题,然后在 SO 上发布您解决问题的问题。很简单,它是如何工作的,嗯??
  • @Robi 不用付钱,我们可以帮你。如果您转到我提供给您的链接并输入Code generator,您就有了脚本。
  • 我有点生疏,愿意帮忙付款
  • 我在 csv 文件中有 25000 行 :(
猜你喜欢
  • 2023-03-12
  • 1970-01-01
  • 2016-08-22
  • 2013-05-12
  • 2020-04-25
  • 1970-01-01
  • 1970-01-01
  • 2022-12-05
  • 2015-01-04
相关资源
最近更新 更多