我有一些这样的字符串
Name xxx Product 1 Pc 100
Name Pci Product2Pc.200
Name Pcx Product 3 Pcs300
我想转PC
来Price
,这是导致我想要
Name xxx Product 1 Price 100
Name Pci Product 2 Price 200
Name Pcx Product 3 Price 300
起初我用
$pattern = array('/(\s*)Product(\s*)/', '/(\s*)(Pc\.?|Pcs)(\s*)/');
但这把我所有的PC都改为Price
Name xxx Product 1 Price 100
Name Price i Product 2 Price 200
Name Price x Product 3 Price 300
这是我的代码。
$pattern = array('/(\s*)Product(\s*)/', '/[^a-z](Pc\.?|Pcs)[^a-z]/');
$replacement = array(' Product ', ' Price ');
$title = preg_replace($pattern, $replacement, $title, -1);
但是结果是这样的
Name xxx Product 1 Price 100
Name Pci Product Price 00
Name Pcx Product 3 Price 00
谢谢。
正则表达式:
(Product)\s*(\d+)\s*Pc[.s]?\s*(\d+)
替换字符串:
$1 $2 Price $3
$string = <<<EOT
Name xxx Product 1 Pc 100
Name Pci Product2Pc.200
Name Pcx Product 3 Pcs300
EOT;
$pattern = "~(Product)\s*(\d+)\s*Pc[.s]?\s*(\d+)~";
echo preg_replace($pattern, "$1 $2 Price $3", $string);
输出:
Name xxx Product 1 Price 100
Name Pci Product 2 Price 200
Name Pcx Product 3 Price 300
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句