<?php
class TestClass {
private $firstArray;
function setFirstArray($inputArray) {
$this->firstArray = explode ( '|', $inputArray, findNum ( $inputArray ) );
}
/*
*找出要截取多少个
*/
function findNum($str) {
$num = 1;
for($i = 0; $i < strlen ( $str ); $i ++) {
if ($str [$i] == '|') {
$num ++;
}
}
echo $num . "<br/>";
return $num;
}
function printStr(){
foreach ($this->firstArray as $x){
echo $x."<br/>";
}
}
}
function findNum1($str) {
$num = 1;
for($i = 0; $i < strlen ( $str ); $i ++) {
if ($str [$i] == '|') {
$num ++;
}
}
return $num;
}
$str = "one|two|three";
echo "testArray2:"."<br/>";
$testArray2 = explode ( '|', $str, findNum1( $str ) );
foreach ( $testArray2 as $x ) {
echo $x."<br/>" ;
}
echo "end testArray2"."<br/>";
$tClass = new TestClass ();
$tClass->setFirstArray ($str);
echo "testArray1:"."<br/>";
$tClass->printStr();
?>为什么输出结果是
testArray2:
one
two
three
end testArray2
也就是,为什么在类中调用explode的时候就执行不了了?
class TestClass {
private $firstArray;
function setFirstArray($inputArray) {
$this->firstArray = explode ( '|', $inputArray, findNum ( $inputArray ) );
}
/*
*找出要截取多少个
*/
function findNum($str) {
$num = 1;
for($i = 0; $i < strlen ( $str ); $i ++) {
if ($str [$i] == '|') {
$num ++;
}
}
echo $num . "<br/>";
return $num;
}
function printStr(){
foreach ($this->firstArray as $x){
echo $x."<br/>";
}
}
}
function findNum1($str) {
$num = 1;
for($i = 0; $i < strlen ( $str ); $i ++) {
if ($str [$i] == '|') {
$num ++;
}
}
return $num;
}
$str = "one|two|three";
echo "testArray2:"."<br/>";
$testArray2 = explode ( '|', $str, findNum1( $str ) );
foreach ( $testArray2 as $x ) {
echo $x."<br/>" ;
}
echo "end testArray2"."<br/>";
$tClass = new TestClass ();
$tClass->setFirstArray ($str);
echo "testArray1:"."<br/>";
$tClass->printStr();
?>为什么输出结果是
testArray2:
one
two
three
end testArray2
也就是,为什么在类中调用explode的时候就执行不了了?
$this->firstArray = explode ( '|', $inputArray, findNum ( $inputArray ) );
}
将上面的方法修改如下
function setFirstArray($inputArray) {
$this->firstArray = explode ( '|', $inputArray,3);
}
或
function setFirstArray($inputArray) {
$this->firstArray = explode ( '|', $inputArray);
}
运行结果:
testArray2:
one
two
three
end testArray2
testArray1:
one
two
three