1

我有一个这样的字符串

$str=44797675962044781781414912/04/2012
  • 前 12 位:447976759620 是电话号码
  • 接下来的 12 位:447817814149 是服务器代码

休息是日期

我试图这样得到它:

echo substr($str,0,12).'<br/>';
echo substr($str,12,24).'<br/>';
$tempRest["receivedtime"]=substr($pieces[0],-10);

得到这个奇怪的输出:

   44797675962
   12/04/2012
   44778691004444781781414912/04/2012

我能做些什么。有什么帮助吗?

编辑:

原始代码:

  function GetSoapReturnArray() {
 $classmap = array('MyWSDLStructure' => 'MyComplexDataType');
 $m_wsdl = 'https://m2mconnect.orange.co.uk/orange-soap/services/MessageServiceByCountry?wsdl';
  $m_arr_soapclient = array('trace' => true, 'exceptions' => true);


  $soap_client_handle = new SoapClient($m_wsdl, $m_arr_soapclient);
  $soap_result = $soap_client_handle->peekMessages('telab048', 'Qwerty12', 40, '', 1);;

  $str=NULL;
  
  foreach ($soap_result as $soapArrayData)
  {
    $pieces = preg_split('/[ ]/', $soapArrayData);

  $str=$pieces[0];
    //echo $str.'<br/>';
    
    echo substr($str, 0, 12) . '<br />';
    echo substr($str, 12, 12) . '<br />';
  }
}


}
  GetSoapReturnArray();

  
4

3 回答 3

4

第二个参数substr是长度,而不是结束索引。通过12而不是24.

echo substr($str, 0, 12) . '<br />';
echo substr($str, 12, 12) . '<br />';
$tempRest["receivedtime"] = substr($pieces[0], 24);
于 2012-04-20T18:54:30.207 回答
0

你遇到了问题,因为$str它不仅仅是一个字符串,它包含HTML用于strip_tags过滤所有 html 标签的标签......

尝试

    foreach ( $soap_result as $soapArrayData ) {
        $pieces = preg_split ( '/[ ]/', $soapArrayData );

        $str = $pieces [0];
        $str = trim ( $str );
        $str = strip_tags ( $str );
        echo substr ( $str, 0, 12 ) . '<br />'; //Phone Number
        echo substr ( $str, 12, 12 ) . '<br />'; // Server Code
        echo substr ( $str, 24, 10 ) . '<br />'; //Time
    }
于 2012-04-20T19:37:12.617 回答
0

为此,您有sscanf

$r = sscanf($str, '%12s%12s%s', $phone, $server, $date);

输出(var_dump($phone, $server, $date);):

string(12) "447976759620"
string(12) "447817814149"
string(10) "12/04/2012"
于 2012-04-20T19:57:49.510 回答