如何替换...一个部分是数字的另一个是另一个是什么?

时间:2021-02-15 14:13:25

how can i put a space between a word where one part is numeric the other is'int? like so:

如何在一个单词是数字的另一个单词之间放置一个空格?像这样:

$string = "this is a 2000px height photo";
$string = preg_replace('???',' //1',$string);
echo $string; //this is a 2000 px height photo

can someone help me? thanks!

有人能帮我吗?谢谢!

1 个解决方案

#1


3  

$string = preg_replace('/(\d+)([[:alpha:]]+)/', '$1 $2', $string);
  • \d+ matches any digit
  • \ d +匹配任何数字

  • [[:alpha:]]+ matches any letter (\w+ is wrong because matches letters and digits: the preg_replace() call splits numbers - e.g. '100' becomes '10 0')
  • [[:alpha:]] +匹配任何字母(\ w +错误,因为匹配字母和数字:preg_replace()调用拆分数字 - 例如'100'变为'10 0')

#1


3  

$string = preg_replace('/(\d+)([[:alpha:]]+)/', '$1 $2', $string);
  • \d+ matches any digit
  • \ d +匹配任何数字

  • [[:alpha:]]+ matches any letter (\w+ is wrong because matches letters and digits: the preg_replace() call splits numbers - e.g. '100' becomes '10 0')
  • [[:alpha:]] +匹配任何字母(\ w +错误,因为匹配字母和数字:preg_replace()调用拆分数字 - 例如'100'变为'10 0')