<?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的时候就执行不了了?
解决方案 »
- 动态循环输出表格的换行问题~~~~~~~
- 救急呀。。。大家有没有用过ZipArchive这个类
- 如何截取出字符串除了最后一个字母的新串?
- 请教一下,如何设计标签体系[整站]
- 读写一个txt文件,以前都挺正常的,在租用的服务器上也挺正常的,可是最近两天,在我自己的机器上总出现下面的错误,请问是怎么回事?
- Apache +php+mysql 为何死活不能显示jpg(pjpeg)格式的图片
- PHP如果想要完成服务器之间的传递文件可以吗?
- syntax error, unexpected '}' 的解决办法?
- 求助!同样的用户名密码 后台登不了
- PHP中如何使用缓存加速?
- php列出文件夹中所有文件,竟然列出了不存在的文件?
- 外网有办法用192.168开头的地址访问吗?
$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