新学PHP,有个小问题不能解决了,关于数组参数传递的
小弟是新手。。。一直未找到以下错误源码如下:
<?php
include 'SmsOperator.php';
$smsOperator = new SmsOperator();
$matches = "
result=0&des=OK&mobile=15211112222&content=KMA&recvtime=2010-07-19 09:49:58&mobile=15222223333&content=one&recvtime=2010-07-14 09:51:28&mobile=15233334444&content=MDMAdsJMD!&recvtime=2010-07-14 09:51:44";
$recv = array();
$recv = $smsOperator->getMo($matches);
for($i=0;$i<strlen($recv);$i++){
echo $recv[$i]->toString();
}
类SmsOperator
<?php
include 'Sms2.php';
class SmsOperator{
private $host = 'www.';
private $port = 8080;
private $noGetMoNum = 0;
private $maxGetMoNum = 30;
private $smsCount = 0;
function getMo($matches){
$i = strpos($matches,'result=', 0);
$j = strpos($matches,'&des=', 0);
if($j >0){
if(($j - $i - 7) <= 0){
$result = -1;
$des = '接收服务器错误!';
}else{
$result = (int)substr($matches,$i+7,$j - $i - 7);
//des
$i = strpos($matches, '&des=');
$j = strpos($matches, '&', $i + 1);
$des = substr($matches,$i+5,$j - $i - 5);
if ($des=='OK'){
$smss = $this->getSms($matches,$smss);
}else{
$des='未接收到短信!';
}
}
}
$smss[0] = new Sms2();
$smss[0]->result = $result;
$smss[0]->des = $des;
return $smss;
}
private function getSms($a,$smss){
$smss[$this->smsCount] = new Sms2();
$i = strpos($a, '&mobile=');
$j = strpos($a, '&content=', $i + 1);
$smss[$this->smsCount]->phone = substr($a, $i+8,$j-$i-8);
$i = strpos($a, '&content=');
$j = strpos($a, '&recvtime=', $i + 1);
$smss[$this->smsCount]->content = substr($a, $i+9,$j-$i-9);
$i = strpos($a, '&recvtime=');
$j = strpos($a, '&', $i + 1);
if($j != NULL){
$smss[$this->smsCount]->tim = substr($a, $i+10,$j-$i-10);
$this->smsCount++;
$a = substr($a,$j);
return $this->getSms($a,$smss);
}
$smss[$this->smsCount]->tim = substr($a, $i+10);
return $smss;
}
}
类Sms2
<?php
class Sms2 {
var $phone = "";
var $content ="";
var $tim = "";
var $result = -1;
var $des = "";
function toString(){
return "result:".$this->result.'<br>'
."des:".$this->des.'<br>'
."phone:".$this->phone.'<br>'
."content:".$this->content.'<br>'
."tim:".$this->tim.'<br>';
}
}
目的很简单,把字符串里的mobile,content,tim取出来,封装为Sms2对象,再装到数组里,作为参数传递出来。。。
不知道哪里数组未定义,请大家帮忙 看看。。。