dongxing1412 2014-10-20 11:28
浏览 4
已采纳

将此值拆分为插入查询的一部分

Is there any way to achieve the following? I need to take this $query and split it into its various elements (the reason is because I am having to reprocess an insert query). As you can see this will work for regular string blocks or numbers, but not where a number, occurs in the string. Is there a way to say |\d but not where that \d occurs within a ' quoted string '?

$query = "('this is\'nt very, funny (I dont think)','is it',12345,'nope','like with 2,4,6')";

$matches = preg_split("#',|\d,#",substr($query,1,-1));

echo $query;   
print'<pre>[';print_r($matches);print']</pre>';

So just to be clear about expected results:

0:'this is\'nt very, funny (I dont think)'
1:'it is'
2:12345
3:'nope'
4:'like with 2,4,6'.

** Additionally I don't mind if each string is not quoted - I can requote them myself.

  • 写回答

5条回答 默认 最新

  • dongxuxian6930 2014-10-20 11:59
    关注

    Could (*SKIP)(*F) parts that are inside single quotes and match , outside:

    '(?:\\'|[^'])*'(*SKIP)(*F)|,
    

    (?:\\'|[^']) Inside the single quotes matches escaped \' or a character that is not a single quote.

    See Test at regex101.com

    $query = "('this is\'nt very, funny (I dont think)','is it',12345,'nope','like with 2,4,6')";
    
    $matches = preg_split("~'(?:\\\\'|[^'])*'(*SKIP)(*F)|,~", substr($query,1,-1));
    
    print_r($matches);
    

    outputs to (test at eval.in)

    Array
    (
        [0] => 'this is\'nt very, funny (I dont think)'
        [1] => 'is it'
        [2] => 12345
        [3] => 'nope'
        [4] => 'like with 2,4,6'
    )
    

    Not absolutely sure, if that is what you mean :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了