Convert comma to semi-colon for the certain part of the string using PHP

541 Views Asked by At

I have a string which is such like,

$input = "div-item-2,4,maintype:social| heading:sdfsdf| social: 1, 2, 3 | table:Social| item:3| align:vertical| style:icon| isactive:1,8,0";

I would like to convert comma to semi-colon between | is start and end. This can be illustrated below by the below image, comma to semi-colon between | is start and end

So the string will be converted as as below,

$output = "div-item-2,4,maintype:social| heading:sdfsdf| social: 1; 2; 3 | table:Social| item:3| align:vertical| style:icon| isactive:1,8,0";

There are some other example is below,

$input = "div-item-0,4,maintype:menu| heading:Quick, Link| table:Menu | menuid:1| align:vertical| style:icon| isactive:1,0,0";

$output = "div-item-0,4,maintype:menu| heading:Quick; Link| table:Menu | menuid:1| align:vertical| style:icon| isactive:1,0,0";

Same case can be below,

$input = "div-item-1,4,maintype:text| heading: Name | title: Learn from here| logo:/photos/20/01.jpg| description: This is some description, so you can stay with us| isactive:1,4,0";

$output= "div-item-1,4,maintype:text| heading: Name | title: Learn from here| logo:/photos/20/01.jpg| description: This is some description; so you can stay with us| isactive:1,4,0";
3

There are 3 best solutions below

0
On BEST ANSWER

You can use this regex to do what you want:

((\||(?<!^)\G)[^,|]*),(?=.*\|)

It looks for

  • either a |, or the start of the string after the previous match (\G),
  • followed by some number of non | or , characters,
  • followed by a ,
  • followed by some number of characters then a |

Demo on regex101

Note that \G would normally also match the start of string, the negative lookbehind (?<!^) prevents it doing that.

The characters between the start of match and the , are captured in group 1, and the replacement string is $1;. In PHP:

echo preg_replace('/((\||(?<!^)\G)[^,|]*),(?=.*\|)/', '$1;', $input);

Demo on 3v4l.org

0
On

try this..

function replaceSTR( $string, $search, $replace, $separator, $start, $end ){

  /*
   * note that this is base on your sample
   * string of course your string
   * search for ,
   * replace it with ;
   * your separator is |
   * in your sample your start is 1
   * end is 3
  */

  $result = array();
  $string = explode( $separator, $string );

  foreach( $string as $key => $value ){
   if( $key < $start || $key > $end ){
    $result[] = $value;
   }else{
    $result[] = str_replace(",",";",$value);
   }
  }
  return implode("|",$result);
}
0
On

Not sure, but maybe this expression might return the desired output:

$re = '/(?:^[^|]*|(?<=\|)(?:[^,]*)(?=\|)|[^|]*$|[^,\r\n]*)|(,)/m';
$str = 'div-item-2,4,maintype:social| heading:sdfsdf| social: 1, 2, 3 | table:Social| item:3| align:vertical| style:icon| isactive:1,8,0
div-item-0,4,maintype:menu| heading:Quick, Link| table:Menu | menuid:1| align:vertical| style:icon| isactive:1,0,0"
div-item-1,4,maintype:text| heading: Name | title: Learn from here| logo:/photos/20/01.jpg| description: This is some description, so you can stay with us| isactive:1,4,0';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);

$output = '';
foreach ($matches as $key => $value) {
    if ($value[1] == ',') {
        $output .= ';';
    } else {
        $output .= $value[0];
    }
}

var_dump($output);

Output

string(413) "div-item-2,4,maintype:social| heading:sdfsdf| social: 1; 2; 3 | table:Social| item:3| align:vertical| style:icon| isactive:1,8,0div-item-0,4,maintype:menu| heading:Quick; Link| table:Menu | menuid:1| align:vertical| style:icon| isactive:1,0,0"div-item-1,4,maintype:text| heading: Name | title: Learn from here| logo:/photos/20/01.jpg| description: This is some description; so you can stay with us| isactive:1,4,0"

If you wish to explore/simplify/modify the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.