愿你能帮助我...... 很多年前我的朋友给了我下面提供的PHP脚本代码,现在我想改变它可以读取编号从零开始
现有的可能只读1:1,10:200等。但我想也可以从01:01,0000002:0120读取......
function is_valid_number($sesuatu)
{
if(preg_match("/^([0-9]+)$/", $sesuatu)) return(TRUE);
else return(FALSE);
}
//--------------
if(isset($teks["chapter"]) && (is_valid_number($teks["chapter"]) === FALSE))
{
if(strcmp($teks["chapter"], "") != 0) echo "".Chapter_need_number.""; //bhs
unset($teks["chapter"]); //Just get rid of it.
}
if(isset($teks["verse"]) && (is_valid_number($teks["verse"]) === FALSE))
{
//There is a non-numerical character in verse. We /should/ be able to examine it for range.
$temp_array = explode("-", $teks["verse"], 2);
if(isset($temp_array[0]) && isset($temp_array[1]) && is_valid_number($temp_array[0]) === TRUE && is_valid_number($temp_array[1]) === TRUE)
{
$teks["verse"] = $temp_array[0];
$teks["verse_end"] = $temp_array[1];
// echo ("<h3>".Verse_range." \"" . $temp_array[0] . "\" - \"" . $temp_array[1] . "\"</h3>\n"); //bhs
if($teks["verse_end"] <= $teks["verse"])
{
echo "".Verse_range_need_larger_ending."";//bhs
unset($teks["verse"]);
unset($teks["verse_end"]);
}
}
else
{
if(strcmp($teks["verse"], "") != 0) echo "".Verse_need_number.""; //bhs
unset($teks["verse"]); //Just get rid of it.
}
}
if(isset($teks["verse"]) && !isset($teks["chapter"]))
{
echo "".Verse_need_chapter.""; //bhs
unset($teks["verse"]); //Just get rid of it.
}
//--------------
谢谢
答案 0 :(得分:1)
您应该使用PHP内部函数,例如is_numeric
和empty
。并避免使用字符串连接与空字符串(无论出于何种原因),而只是确保这些常量(我猜它们是常量)是兼容的,一切都很好。
if ( isset( $teks['chapter'] ) && !is_numeric( $teks['chapter' ) )
{
if ( !empty( $teks['chapter'] ) )
echo Chapter_need_number;
unset( $teks['chapter'] );
}
if ( isset( $teks['verse'] ) && !is_numeric( $teks['verse'] ) )
{
$temp_array = explode( '_', teks['verse'], 2 );
if ( count( $temp_array ) >= 2 && is_numeric( $temp_array[0] ) && is_numeric( $temp_array[1] ) )
{
$teks['verse'] = intval( $temp_array[0] );
$teks['verse_end'] = intval( $temp_array[1] );
if ( teks['verse_end'] <= teks['verse'] )
{
echo Verse_range_need_larger_ending;
unset( $teks['verse'] );
unset( $teks['verse_end'] );
}
}
else
{
if ( !empty( $teks['verse'] ) )
echo Verse_need_number;
unset( $teks['verse'] );
}
}
if ( isset( $teks['verse'] ) && !isset( $teks['chapter'] ) )
{
echo Verse_need_chapter;
unset( $teks['verse'] );
}
答案 1 :(得分:0)
我会在这里猜测,因为我不太明白这个问题,但我认为你应该改变这个:
if($teks["verse_end"] <= $teks["verse"])
到这个
if(intval($teks["verse_end"]) <= intval($teks["verse"]))