使用 PHP 将字符串的特定部分的逗号转换为分号

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

我有一个这样的字符串,

$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";

我想将 | 之间的逗号转换为分号是开始和结束。这可以通过下图说明,

所以字符串将被转换如下,

$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";

下面还有一些其他的例子,

$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";

同样的情况可以在下面,

$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";

试试这个..

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);
}

不确定,但也许这个表达式可能 return 所需的输出:

$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);

输出

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.


您可以使用此正则表达式来执行您想要的操作:

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

它寻找

  • 要么是 |,要么是前一个匹配项 (\G) 之后的字符串的开头,
  • 后跟一些非 |, 字符,
  • 后跟 ,
  • 后跟一些字符,然后是 |

Demo on regex101

请注意 \G 通常也会匹配字符串的开头,负向回顾 (?<!^) 会阻止它这样做。

匹配开始到,之间的字符在第1组中被捕获,替换字符串为;。在 PHP:

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

Demo on 3v4l.org