function sub($url_cut_start,$url_cut_start,$out_rp,$out){
foreach($out_rp[1] as $outurl){
//echo $outurl."</br>";
if($url_cut_start==""){
echo "请输入截取连接起始位置";
}
if($url_cut_start!=""&&$url_cut_length!=""){
$sub=substr($outurl,$url_cut_start,$url_cut_length);
echo $sub."</br>";//截取部分
}
if($url_cut_length==""){
$sub=substr($outurl,$url_cut_start);
echo $sub."</br>";
}
$allurl=$out.$sub;
echo '<a href="'.$allurl.'">'.$allurl.'</a></br>';
return $allurl;
}
}我就是想调用完这个方法后,得到$allurl
但$allurl不是一个值,我不知该怎么办了
foreach($out_rp[1] as $outurl){
//echo $outurl."</br>";
if($url_cut_start==""){
echo "请输入截取连接起始位置";
}
if($url_cut_start!=""&&$url_cut_length!=""){
$sub=substr($outurl,$url_cut_start,$url_cut_length);
echo $sub."</br>";//截取部分
}
if($url_cut_length==""){
$sub=substr($outurl,$url_cut_start);
echo $sub."</br>";
}
$allurl=$out.$sub;
echo '<a href="'.$allurl.'">'.$allurl.'</a></br>';
return $allurl;
}
}我就是想调用完这个方法后,得到$allurl
但$allurl不是一个值,我不知该怎么办了
<?php
function sub($url_cut_start, $url_cut_length=0, $out_rp, $out){
$allurl = "";
foreach($out_rp[1] as $outurl){
// echo $outurl."</br>";
if($url_cut_start==""){
echo "请输入截取连接起始位置";
}
if($url_cut_start!=""&&$url_cut_length!=0){
$sub=substr($outurl,$url_cut_start,$url_cut_length);
// echo $sub."</br>";//截取部分
}
if($url_cut_length==0){
$sub=substr($outurl,$url_cut_start);
// echo $sub."</br>";
}
$allurl .=$out.$sub."\n";
// echo '<a href="'.$allurl.'">'.$allurl.'</a></br>';
}
return $allurl;
}$arr = array(
1=>array(
0=>'http://www.google.com/?fs=productlist&id=2134314',
1=>'http://www.google.com/?fs=productlist&id=2134315',
2=>'http://www.google.com/?fs=productlist&id=2134316',
)
);
echo strlen('http://www.google.com/?fs=productlist&id=');
$out = '?fs=productlist&id=';
echo sub(41, null, $arr, $out);
?>循环外初始化变量 再用 .= 即可
isset($url_cut_start) ? $url_cut_start : "";
isset($url_cut_length) ? $url_cut_length : "";
$allurl = "";
foreach($out_rp[1] as $outurl){
if($url_cut_start!=""){
if ($url_cut_length != "")
$sub=substr($outurl,$url_cut_start,$url_cut_length);
else
$sub=substr($outurl,$url_cut_start);
}else
echo "请输入截取连接起始位置";
$allurl .=$out.$sub."\n";
}
return $allurl;
}
//echo '<a href="'.$allurl.'">'.$allurl.'</a></br>';
想问一下
1、
$allurl返回的不止一个数,还是在foreach循环里面,只要return $allurl
在调用sub($url_cut_start, $url_cut_length=0, $out_rp, $out)方法就可以得到一系列数么$out_rp是我用preg_match_all($pattern,$contents,$out_rp)得到的数组
2、 isset($url_cut_start) ? $url_cut_start : "";怎么解释哈
我查到的是:
判断变量是否已配置。
语法: int isset(mixed var);怎么会有?:这么用哈,什么意思呢
function sub($url_cut_start, $url_cut_length, $out_rp, $out){
isset($url_cut_start) ? $url_cut_start : "";//等于:if ($url_cut_start) $url_cut_start = $url_cut_start; else $url_cut_start = "";
isset($url_cut_length) ? $url_cut_length : "";
$allurl = "";
foreach($out_rp[1] as $outurl){
if($url_cut_start!=""){
if ($url_cut_length != "")
$sub=substr($outurl,$url_cut_start,$url_cut_length);
else
$sub=substr($outurl,$url_cut_start);
}else
return "请输入截取连接起始位置";
$allurl .=$out.$sub."\n";
}
return $allurl;
}$arr = array(
1=>array(
0=>'<a href="http://www.google.com/?fs=productlist&id=2134314">2134314</a>',
1=>'<a href="http://www.google.com/?fs=productlist&id=2134315">2134315</a>',
2=>'<a href="http://www.google.com/?fs=productlist&id=2134316">2134316</a>',
)
);
$contents = "";
foreach($arr[1] as $s)
$contents .= $s . "\n";
$pattern = "/<a href=\"(.*?)\">.*<\/a>/i";
preg_match_all($pattern,$contents,$out_rp);
$out = '?fs=productlist&id=';
echo sub(strlen('http://www.google.com/?fs=productlist&id='), null, $out_rp, $out);
echo sub(strlen('http://www.google.com/?fs=productlist&id='), 3, $out_rp, $out);
echo sub(null, 3, $out_rp, $out);