0

Ive got an sql dump and wanna loop through the rows then change, delete and insert specific field(s) in each row. Now I need to replace all occurrences of the sql ',' that starts and ends each fields value.

I only need help for my last problem. I need to replace all commas that have a digit number before and after the comma with my splitter #***#.

I found this regex, /(^\d,\d)*$/ but some how it does not do the job! I have fields with text that include comma so I dont wanna replace those ofcause

$sqlrows = str_replace(",",'#***#',preg_match('/(^\d,\d)*$/', $sqlrows)); 
// defines sql dump regex [0-9],[0-9]
Thorn G
  • 12,620
  • 2
  • 44
  • 56
Jakob
  • 13
  • 3

2 Answers2

0

You should use preg_replace() rather than str_replace() something like this:

$sqlrows = preg_replace( '/(^\d,\d)*$/', "#***#", $sqlrows );
bitfiddler
  • 2,095
  • 1
  • 12
  • 11
0

"I need to replace all commas that have a digit number before and after the comma with my splitter #*#."

Try this approach:

<?php
$str = '1,2
3,4
';
$str = preg_replace('/(\d),(\d)/', '$1#***#$2', $str);

print $str;

prints:

1#***#2
3#***#4

I am finding all the commas, surrounded by digits, and replace them with your separator, keeping the digits intact by using backreferences.

user4035
  • 22,508
  • 11
  • 59
  • 94