如何在一行中编写满足以下要求的正则表达式

时间:2018-10-03 19:15:57

标签: regex escaping special-characters

i/p array: 1. '"{aaBbCc"'
           2. '"aaBbCc"'

预期的o / p:Aa Bb Cc

我的代码:

foreach ($input as $ip) {        
  $fieldName = str_replace("{", "", $ip);
  $fieldName = preg_replace("/(?<!\A)[A-Z]+/", ' $0', $fieldName);
  $fieldName = ucwords(str_replace(["'", "\"", "&quot;"], "", $fieldName))
 }

我需要一个模式,以便可以将所有内容放到preg_replace中。请说明一下模式

2 input array 1. ' "\"Tennessee\" OR \"North Carolina\""'
              2. '"total_amount"'
              3. '"Yes"}'

预期o / p:“田纳西州”或“北卡罗来纳州”,“总金额”,“是”

我的代码:

foreach ($input2 as $ip2) {
 $criteria = preg_replace('/[^a-zA-Z0-9_ %\[\]\.\(\)%&-]/s', '', $ip2);
 $criteria = substr($criteria,5, -5); //for some reason double quotes come as &quotes so substring 5. 
 //I did try trim
}

//this may contain other specail characters in name like ampersand, hyphen.in end I want double quotes around each word and split the string with comma delimiter.
3. $str = '{"name":"\"PAA Mgmnt , LLC\" OR \"48 S. 4th, LLC\" OR \"ABC Home Furnishings, Inc.\"","range":"total_sales"}'
 req o/p: "name": "PAA Mgmnt , LLC" OR "48 S. 4th, LLC"OR "ABC Home Furnishings, Inc."
          "range": "total_sales"

我确实在网站上搜索了正则表达式教程,但仍然很难编写它。我想将我的所有代码组合成一种针对每种需求的模式。

0 个答案:

没有答案