PHP代码可以工作但不在函数内部

时间:2014-07-17 11:05:42

标签: php function

我使用text2re.com生成了匹配15位IMEI号码的代码。 代码工作得很好但是当插入到一个函数中时,我无法正常工作。

PHP函数

<?php
function matchIMEI($str)
{
  $re1='.*?';   # Non-greedy match on filler
  $re2='\\d';   # Uninteresting: d
  $re3='.*?';   # Non-greedy match on filler
  $re4='\\d';   # Uninteresting: d
  $re5='.*?';   # Non-greedy match on filler
  $re6='\\d';   # Uninteresting: d
  $re7='.*?';   # Non-greedy match on filler
  $re8='\\d';   # Uninteresting: d
  $re9='.*?';   # Non-greedy match on filler
  $re10='\\d';  # Uninteresting: d
  $re11='.*?';  # Non-greedy match on filler
  $re12='\\d';  # Uninteresting: d
  $re13='.*?';  # Non-greedy match on filler
  $re14='\\d';  # Uninteresting: d
  $re15='.*?';  # Non-greedy match on filler
  $re16='\\d';  # Uninteresting: d
  $re17='.*?';  # Non-greedy match on filler
  $re18='(\\d)';    # Any Single Digit 1
  $re19='(\\d)';    # Any Single Digit 2
  $re20='(\\d)';    # Any Single Digit 3
  $re21='(\\d)';    # Any Single Digit 4
  $re22='(\\d)';    # Any Single Digit 5
  $re23='(\\d)';    # Any Single Digit 6
  $re24='(\\d)';    # Any Single Digit 7
  $re25='(\\d)';    # Any Single Digit 8
  $re26='(\\d)';    # Any Single Digit 9
  $re27='(\\d)';    # Any Single Digit 10
  $re28='(\\d)';    # Any Single Digit 11
  $re29='(\\d)';    # Any Single Digit 12
  $re30='(\\d)';    # Any Single Digit 13
  $re31='(\\d)';    # Any Single Digit 14
  $re32='(\\d)';    # Any Single Digit 15

  if ($c=preg_match_all ("/".$re1.$re2.$re3.$re4.$re5.$re6.$re7.$re8.$re9.$re10.$re11.$re12.$re13.$re14.
                         $re15.$re16.$re17.$re18.$re19.$re20.$re21.$re22.$re23.$re24.$re25.$re26.$re27.
                         $re28.$re29.$re30.$re31.$re32."/is", $str, $matches))
  {
      $d1=$matches[1][0];
      $d2=$matches[2][0];
      $d3=$matches[3][0];
      $d4=$matches[4][0];
      $d5=$matches[5][0];
      $d6=$matches[6][0];
      $d7=$matches[7][0];
      $d8=$matches[8][0];
      $d9=$matches[9][0];
      $d10=$matches[10][0];
      $d11=$matches[11][0];
      $d12=$matches[12][0];
      $d13=$matches[13][0];
      $d14=$matches[14][0];
      $d15=$matches[15][0];
      return "$d1$d2$d3$d4$d5$d6$d7$d8$d9$d10$d11$d12$d13$d14$d15";
  }

}


?>

调用函数的代码:

include function.php    
$txt='2014-07-17 IMEI: 016666001643648 USA MODEL';
    $test = matchIMEI($txt);
    echo $test;

PHP的新手。可能有什么不对?

2 个答案:

答案 0 :(得分:0)

您想对您创建的IMEI函数使用PHP include函数:

的index.php:

<?php
include 'imei_func.php';

$txt='2014-07-17 IMEI: 016666001643648 USA MODEL';
$test = matchIMEI($txt);
echo $test;

?>

imei_func.php

<?php

function matchIMEI($str)
{
  $re1='.*?';       # Non-greedy match on filler
  $re2='\\d';       # Uninteresting: d
  $re3='.*?';       # Non-greedy match on filler
  $re4='\\d';       # Uninteresting: d
  $re5='.*?';       # Non-greedy match on filler
  $re6='\\d';       # Uninteresting: d
  $re7='.*?';       # Non-greedy match on filler
  $re8='\\d';       # Uninteresting: d
  $re9='.*?';       # Non-greedy match on filler
  $re10='\\d';      # Uninteresting: d
  $re11='.*?';      # Non-greedy match on filler
  $re12='\\d';      # Uninteresting: d
  $re13='.*?';      # Non-greedy match on filler
  $re14='\\d';      # Uninteresting: d
  $re15='.*?';      # Non-greedy match on filler
  $re16='\\d';      # Uninteresting: d
  $re17='.*?';      # Non-greedy match on filler
  $re18='(\\d)';    # Any Single Digit 1
  $re19='(\\d)';    # Any Single Digit 2
  $re20='(\\d)';    # Any Single Digit 3
  $re21='(\\d)';    # Any Single Digit 4
  $re22='(\\d)';    # Any Single Digit 5
  $re23='(\\d)';    # Any Single Digit 6
  $re24='(\\d)';    # Any Single Digit 7
  $re25='(\\d)';    # Any Single Digit 8
  $re26='(\\d)';    # Any Single Digit 9
  $re27='(\\d)';    # Any Single Digit 10
  $re28='(\\d)';    # Any Single Digit 11
  $re29='(\\d)';    # Any Single Digit 12
  $re30='(\\d)';    # Any Single Digit 13
  $re31='(\\d)';    # Any Single Digit 14
  $re32='(\\d)';    # Any Single Digit 15

  if ($c=preg_match_all ("/".$re1.$re2.$re3.$re4.$re5.$re6.$re7.$re8.$re9.$re10.$re11.$re12.$re13.$re14.$re15.$re16.$re17.$re18.$re19.$re20.$re21.$re22.$re23.$re24.$re25.$re26.$re27.$re28.$re29.$re30.$re31.$re32."/is", $str, $matches))
  {
    $d1=$matches[1][0];
    $d2=$matches[2][0];
    $d3=$matches[3][0];
    $d4=$matches[4][0];
    $d5=$matches[5][0];
    $d6=$matches[6][0];
    $d7=$matches[7][0];
    $d8=$matches[8][0];
    $d9=$matches[9][0];
    $d10=$matches[10][0];
    $d11=$matches[11][0];
    $d12=$matches[12][0];
    $d13=$matches[13][0];
    $d14=$matches[14][0];
    $d15=$matches[15][0];
    return "$d1$d2$d3$d4$d5$d6$d7$d8$d9$d10$d11$d12$d13$d14$d15";
  }
}
?>

答案 1 :(得分:0)

  

我认为这将是获得正确的简短而快捷的方法   字符串。

$txt='2014-07-17 IMEI: 016666001643648 USA MODEL';
list ($a,$b) =explode(':',$txt);
echo substr($b, 0, 16);
  

希望它可以帮到你