1

我有一个这样的字符串,

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

3 回答 3

2

你可以使用这个正则表达式来做你想做的事:

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

它寻找

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

正则表达式 101 上的演示

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

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

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

3v4l.org 上的演示

于 2019-08-31T05:16:51.433 回答
1

尝试这个..

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);
}
于 2019-08-31T05:03:36.423 回答
0

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

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

如果您想探索/简化/修改表达式,它已在 regex101.com的右上角面板中进行了说明。如果您愿意,您还可以在此链接中观看它如何与一些示例输入匹配。


于 2019-08-31T05:15:50.730 回答