0

愿你能帮助我...多年前我的朋友给了我下面介绍的 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.
 }

//--------------

谢谢你

4

2 回答 2

1

您应该使用 php 内部函数,例如is_numericempty。并且避免将字符串连接与空字符串(无论出于何种原因)一起使用,而只是确保这些常量(我猜它们是常量)是兼容的并且一切都很好。

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'] );
}
于 2009-12-26T17:24:38.087 回答
0

我会在这里大胆猜测,因为我不太明白这个问题,但我认为你应该改变这个:

if($teks["verse_end"] <= $teks["verse"])

对此

if(intval($teks["verse_end"]) <= intval($teks["verse"]))
于 2009-12-26T17:05:14.007 回答