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,
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";
You can use this regex to do what you want:
((\||(?<!^)\G)[^,|]*),(?=.*\|)
It looks for
|
, or the start of the string after the previous match (\G
),|
or ,
characters, ,
|
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);