I've tried a bunch of stuff but nothing works. I have a string and I want to split the string by a comma ,
but don't split if the comma is between a bracket , a brace , or a double or single quotation mark - for this specific case to maintain the complete iframe (which is destroying everything) string. Below is the string ...
class : putmeincoach,
id : random_id,
responsive : { [ type= text ; html = "< iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps/myplaces?ctz=480&ie=UTF8&ll=34.009394,-118.488514&spn=0.020687,0.020921&t=m&z=16"> < /iframe >" ; ] }
I just want
Array (
[0] = 'class : putmeincoach',
[1] = 'id : random_id',
[2] = 'responsive : { [ type= text ; html = "< iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps/myplaces?ctz=480&ie=UTF8&ll=34.009394,-118.488514&spn=0.020687,0.020921&t=m&z=16"> < /iframe >" ; ] } '
Can I do this using preg_split or preg_match with some generalized regex pattern? Lemme also mention that not having the comma between the double quotation marks is really the most important
In PHP:
<?php
$input = '
class : putmeincoach,
id : random_id,
responsive : { [ type= text ; html = "< iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps/myplaces?ctz=480&ie=UTF8&ll=34.009394,-118.488514&spn=0.020687,0.020921&t=m&z=16"> < /iframe >" ; ] },
testsinglequotes: \'hello world\',
testdoublequotes: "hello hello"';
$pattern = '/
\b(\w+)\s*: # capture the word before the colon
\s*( # start capture group match after the colon
{[^}]*}\s*| # match everything between braces OR
"[^"]*"\s*| # match everything between double quotes OR
\'[^\']*\'\s*| # match everything between single OR
[^,]* # match everything up to the comma
) # end capture group
(?:,|$) # match comma or end of string (non-capture group)
/x';
preg_match_all($pattern ,$input, $matches);
print_r($matches);
?>
Output PHP 5.3.13:
Array
(
[0] => Array
(
[0] => class : putmeincoach,
[1] => id : random_id,
[2] => responsive : { [ type= text ; html = "< iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps/myplaces?ctz=480&ie=UTF8&ll=34.009394,-118.488514&spn=0.020687,0.020921&t=m&z=16"> < /iframe >" ; ] },
[3] => testsinglequotes: 'hello world',
[4] => testdoublequotes: "hello hello"
)
[1] => Array
(
[0] => class
[1] => id
[2] => responsive
[3] => testsinglequotes
[4] => testdoublequotes
)
[2] => Array
(
[0] => putmeincoach
[1] => random_id
[2] => { [ type= text ; html = "< iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps/myplaces?ctz=480&ie=UTF8&ll=34.009394,-118.488514&spn=0.020687,0.020921&t=m&z=16"> < /iframe >" ; ] }
[3] => 'hello world'
[4] => "hello hello"
)
)
Regex is your friend
I tried
^(class.*?,).*?(id.*?,).*?(responsive.*,?)$
using http://www.gskinner.com/RegExr/ and this worked. The tricky thing is to use the lazy matching for .* in the first two groups and for the ',' in the last.
You will need to adapt this to PHP since the regex handling is a bit different, I think.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.