Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP将字符串的特定部分的逗号转换为分号_Php_Regex_String_Comma_Automatic Semicolon Insertion - Fatal编程技术网

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

使用PHP将字符串的特定部分的逗号转换为分号,php,regex,string,comma,automatic-semicolon-insertion,Php,Regex,String,Comma,Automatic Semicolon Insertion,我有一根绳子,它是这样的 $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"; 我想将逗号转换为分号,介于| is start和end之间。下图可以说明这一点, 所以字符串的转换如下所示 $output = "div-item-2,4,maintype:social|

我有一根绳子,它是这样的

$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";
我想将逗号转换为分号,介于| is start和end之间。下图可以说明这一点,

所以字符串的转换如下所示

$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);
}
函数replaceSTR($string、$search、$replace、$separator、$start、$end){
/*
*请注意,这是基于您的样本
*绳子当然是你的绳子
*搜寻,
*换成,;
*你的分离器是|
*在您的示例中,您的起点是1
*终点是3
*/
$result=array();
$string=explode($separator,$string);
foreach($key=>$value的字符串){
如果($key<$start | |$key>$end){
$result[]=$value;
}否则{
$result[]=str_replace(“,”,“;”,$value);
}
}
返回内爆(“|”,$result);
}

不确定,但此表达式可能会返回所需的输出:

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

如果您希望探索/简化/修改该表达式,它已被删除 在的右上面板上进行了说明 . 如果你愿意,你可以 也可以观看,它将如何匹配 对照一些样本输入



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

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

< /p>使用“代码> PrggRePosieEclipse <代码>,它与管道之间的部分匹配,并在替换回调函数中使用<代码> STRTR /CUT>。您还可以考虑将其转换成数组,然后在[1 ]到[5 ]位置上执行Strux替换。

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