简体   繁体   English

PHP-替换出现在两个数字之间的字符串

[英]PHP- replace strings where they appear between two numbers

I have strings that look like the following: 我的字符串如下所示:

  • 3/2 Johns St 3/2约翰斯街
  • 3,2 Johns St 约翰斯街3,2号
  • 3 2 Johns St 3 2约翰斯街

I want to replace any occurance of / , , and 我想替换/ ,和的任何出现 with a \\ when they appear in between 2 numbers. 当它们出现在2个数字之间时,用\\表示。 So the above would all end up looking like 3\\2 Johns St . 因此,上述所有内容最终看起来像是3\\2 Johns St What is the easiest way to do this with PHP? 用PHP做到这一点的最简单方法是什么?

使用preg_replace('/(\\d)[\\/, ](\\d)/', '$1\\\\\\\\$2', $string);

Try this : 尝试这个 :

$str  = '3/2 Johns St';
echo $str_new  = preg_replace('/(\d)([^\d])+(\d)/','$1\\\\$3',$str);

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

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