【问题标题】:Match strings starts and end with particular character in a String using PHP? [closed]使用PHP匹配字符串以字符串中的特定字符开头和结尾? [关闭]
【发布时间】:2016-04-28 11:25:43
【问题描述】:

这是我的字符串。总 json 响应以字符串形式出现。任务是识别子域和评论之后的单词。

{item_type:a,custom_domain:"google.com",subdomain:analytics,duration:324.33, id:2892928, comment:goahead,domain_verified:yes}, {item_type:b,custom_domain:"yahoo.com",子域:新闻,评论:真棒,域验证:否},{item_type:c,custom_domain:“amazon.com”,子域:aws,宽度:221,image_id:3233,高度:13,评论:继续,域验证:否},{item_type:d,custom_domain:"facebook.com",subdomain:m,slug:sure,domain_verified:yes}

输出应该是这样的,

analytics, goahead
news, awesome
aws, keep it up
m, sure

简单地说,我需要以 ^subdomain: 开头并以逗号结尾的单词,然后是以 ^comment: 开头并以逗号结尾的单词。

传入的字符串包含大量数据。每个字符串都将包含数千个子域和 cmets。我已经尝试过 preg_match_all 方法。但是我没有找到正确的方法。

【问题讨论】:

  • 你能告诉我们你已经做了什么吗?
  • 我在第 4 个 JSON 中看不到 comment
  • 您肯定已经成为会员足够长的时间了,意识到这既不是方法也不是在这里提出问题的类型?
  • 是的。我不应该以非常模糊的方式提出这个问题。你要我记下这个问题吗?但我得到了我需要的答案。

标签: php regex string


【解决方案1】:

我看到了 3 种方式(我不确定哪种方式的性能最好,但我会打赌最后一种程序方式):

  1. 使用json_decode 函数,您将从字符串中获取一个数组,然后对其进行迭代以获取数据
  2. 使用正则表达式,参见an example here 与模式/subdomain:(.*?),.*?comment:(.*?),/
  3. 使用程序函数,例如:

    $subdomains = [];
    $comments = [];
    
    $subdomainLen = strlen('subdomain:');
    $commentLen = strlen('comment:');
    
    $str = '{item_type:a,custom_domain:"google.com",subdomain:analytics,duration:324.33, id:2892928, comment:goahead,domain_verified:yes}, {item_type:b,custom_domain:"yahoo.com",subdomain:news,comment:awesome,domain_verified:no}, {item_type:c,custom_domain:"amazon.com",subdomain:aws,width:221,image_id:3233,height:13, comment:keep it up,domain_verified:no}, {item_type:d,custom_domain:"facebook.com",subdomain:m,slug:sure,domain_verified:yes}';
    
    // While we found the 'subdomain' pattern
    while(($subdomainPos = strpos($str, 'subdomain')))
    {
        // Removes all char that are behind 'subdomain'
        $str = substr($str, $subdomainPos + $subdomainLen);
    
        // Retrieves the subdomain str and push to array
        $subdomains[] = substr($str, 0, strpos($str, ','));
    
        // If pattern 'comment' exists, do the same as before to extract the comment
        if($commentPos = strpos($str, 'comment'))
        {
            $str = substr($str, $commentPos + $commentLen);
            $comments[] = substr($str, 0, strpos($str, ','));
        }
    }
    

【讨论】:

    【解决方案2】:

    为您提供字符串示例,您可以使用以下正则表达式来捕获所有子域:

    /(subdomain:)[\w|\s]+,/gm
    

    还有:

    /(comment:)[\w|\s]+,/gm
    

    捕获 cmets。

    这是子域的working example

    如果只想要子域的内容或评论,您可以将它们从匹配结果中删除。

    【讨论】:

      【解决方案3】:

      试试这个代码...这里是LIVE EXAMPLE

      <?php 
      $string ='{item_type:a,custom_domain:"google.com",subdomain:analytics,duration:324.33, id:2892928, comment:goahead,domain_verified:yes}, {item_type:b,custom_domain:"yahoo.com",subdomain:news,comment:awesome,domain_verified:no}, {item_type:c,custom_domain:"amazon.com",subdomain:aws,width:221,image_id:3233,height:13, comment:keep it up,domain_verified:no}, {item_type:d,custom_domain:"facebook.com",subdomain:m,slug:sure,domain_verified:yes}';
      $v1= explode(',',str_replace("}","",str_replace("{","",$string)));
      $result =array();
      
      foreach($v1 as $key=>$val)
      {
          $v2 = explode(':',$val);
          if(trim($v2[0])=='subdomain' || trim($v2[0])=='comment')
          {
              $result[]= $v2[1];
          }
      }
      echo implode(',',$result);
      ?>
      

      这将输出:

      analytics,goahead,news,awesome,aws,keep it up,m
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-01-02
        • 2016-12-21
        • 2021-12-17
        • 2021-11-06
        • 2017-11-08
        • 2016-12-07
        • 2012-09-13
        相关资源
        最近更新 更多