2015-05-18 63 views
-1

我试图从一个字符串中取一个数字,除以3,然后用输出替换原来的数字。用var替换另一个数字

$original = "55 dogs"; 
preg_replace("/[^0-9]/","",$original); 

$str = $original/ 3; 
round($str); 

str_replace(numbers, newNumbers, $str); 
echo $str; 

我发现通过谷歌的str_replace,但我不知道那是什么,我想实现的正确途径。如果有人知道一种方法,我会很感激。

+0

什么是你的代码数量和newNumbers? – Saty

+0

你的代码片段没有意义。它在整个地方使用未定义的变量。 – PeeHaa

+0

http://php.net/str_replace – Saty

回答

1

你可以做到这一点使用preg_replace_callback():

$original = "55 dogs"; 
$result = preg_replace_callback(
      '/(\d+)/', 
      function($match) { 
       // You can do whatever you want to do with the match here 
       return round($match[0]/3); 
      }, 
      $original 
    ); 
var_dump($result); // string(7) "18 dogs"