简体   繁体   English

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

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

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它寻找

  • either a |要么| , or the start of the string after the previous match ( \G ), ,或上一个匹配( \G )之后的字符串的开头,
  • followed by some number of non |后跟一些非| or , characters,,字符,
  • followed by a ,后跟一个,
  • followed by some number of characters then a |后跟一些字符,然后是|

Demo on regex101正则表达式 101 上的演示

Note that \G would normally also match the start of string, the negative lookbehind (?<!^) prevents it doing that.请注意, \G通常也会匹配字符串的开头,否定的lookbehind (?<!^)会阻止它这样做。

The characters between the start of match and the , are captured in group 1, and the replacement string is $1;匹配开始和,之间的字符被捕获在第 1 组中,替换字符串是$1; . . In PHP:在 PHP 中:

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

Demo on 3v4l.org 3v4l.org 上的演示

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

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 .如果您想探索/简化/修改表达式,它已在regex101.com的右上角面板中进行了说明。 If you'd like, you can also watch in this link , how it would match against some sample inputs.如果您愿意,您还可以在此链接中观看它如何与一些示例输入匹配。


声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM