Useing A regular expression help please

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
BrettCarr
Forum Newbie
Posts: 22
Joined: Fri Feb 12, 2010 6:45 pm

Useing A regular expression help please

Post by BrettCarr »

Im tring to change this string
"2010-03-14,74527,645,New,7417417,Toll,2,74527417,32,202,7452741741,ghtgbwt,gtgwtgwtfgwtrg,2010-03-14,857527527,645,New,7417418,Toll,7,74527418,32,202,527527,hjm,djmd,mdjmdjmdgjm,2010-03-14,85285,645,New,7417419,Toll,2,74527419,32,202,750757,j,ryjkryjkfryk,yjmyjyjtyjhm";
to this
'[["2010-03-14","74527","645","New","7417417","Toll","2","74527417","32","202","7452741741","ghtgbwt","gtgwtgwtfgwtrg"],["2010-03-14","857527527","645","New","7417418","Toll","7","74527418","32","202","527527","hjm,djmd","mdjmdjmdgjm"]]';*/
For every 12 commas and one word Ide like to add the square brackets Is this posible Im brand new to regular expressions

Code: Select all

<?php
//what i want = '[["2010-03-14","74527","645","New","7417417","Toll","2","74527417","32","202","7452741741","ghtgbwt","gtgwtgwtfgwtrg"],["2010-03-14","857527527","645","New","7417418","Toll","7","74527418","32","202","527527","hjm,djmd","mdjmdjmdgjm"]]';*/
 
$teststring = "2010-03-14,74527,645,New,7417417,Toll,2,74527417,32,202,7452741741,ghtgbwt,gtgwtgwtfgwtrg,2010-03-14,857527527,645,New,7417418,Toll,7,74527418,32,202,527527,hjm,djmd,mdjmdjmdgjm,2010-03-14,85285,645,New,7417419,Toll,2,74527419,32,202,750757,j,ryjkryjkfryk,yjmyjyjtyjhm";
$teststring1 =  preg_replace("/([a-zA-Z0-9_]+?),{12}/" , "\"$1\"],[",$teststring);
 
var_dump($teststring1);
?>
BrettCarr
Forum Newbie
Posts: 22
Joined: Fri Feb 12, 2010 6:45 pm

Re: Useing A regular expression help please

Post by BrettCarr »

BrettCarr wrote:
Im tring to change this string
"2010-03-14,74527,645,New,7417417,Toll,2,74527417,32,202,7452741741,ghtgbwt,gtgwtgwtfgwtrg,2010-03-14,857527527,645,New,7417418,Toll,7,74527418,32,202,527527,hjm,djmd,mdjmdjmdgjm,2010-03-14,85285,645,New,7417419,Toll,2,74527419,32,202,750757,j,ryjkryjkfryk,yjmyjyjtyjhm";
to this
'[["2010-03-14","74527","645","New","7417417","Toll","2","74527417","32","202","7452741741","ghtgbwt","gtgwtgwtfgwtrg"],["2010-03-14","857527527","645","New","7417418","Toll","7","74527418","32","202","527527","hjm,djmd","mdjmdjmdgjm"]]';*/
For every 12 commas and one word Ide like to add the square brackets Is this posible Im brand new to regular expressions

Code: Select all

<?php
//what i want = '[["2010-03-14","74527","645","New","7417417","Toll","2","74527417","32","202","7452741741","ghtgbwt","gtgwtgwtfgwtrg"],["2010-03-14","857527527","645","New","7417418","Toll","7","74527418","32","202","527527","hjm,djmd","mdjmdjmdgjm"]]';*/
 
$teststring = "2010-03-14,74527,645,New,7417417,Toll,2,74527417,32,202,7452741741,ghtgbwt,gtgwtgwtfgwtrg,2010-03-14,857527527,645,New,7417418,Toll,7,74527418,32,202,527527,hjm,djmd,mdjmdjmdgjm,2010-03-14,85285,645,New,7417419,Toll,2,74527419,32,202,750757,j,ryjkryjkfryk,yjmyjyjtyjhm";
$teststring1 =  preg_replace("/([a-zA-Z0-9_]+?),{12}/" , "\"$1\"],[",$teststring);
 
var_dump($teststring1);
?>
Bassically i need to find 13 commas do somthing and repeat until at the end of the string
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Useing A regular expression help please

Post by requinix »

Try explode + array_chunk + (foreach + implode) + another implode.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Useing A regular expression help please

Post by John Cartwright »

See viewtopic.php?p=598788#p598788

.. and why are you quoting yourself when you are making a new post. It's confusing and unusual.
Post Reply