0

我想使用 PHP 中的 substr 方法从文本文件中提取 2 条信息。一些示例行如下

 CON8101 Residential Building/Estimating 6 hrs/w 
 CON8411 Construction Materials I 4 hrs/w
 CON8430 Computers and You 4 hrs/w

我需要提取每门课程所需的小时数。我写的函数从行尾提取数字是否正确?我想出了这个功能:

            function GetCourseHours($couseList)
    {//using 7 character placements from the end, taking 1 value (number)
        $courseHours = substr(courseList, -7, 1);
    }

接下来,提取除 hrs/w 之外的所有内容,例如CON8101 Residential Building/Estimating。我不确定如何在 substr 中表达以从行中提取最后 7 个字符空格之外的所有内容。仅获取课程名称和 ID。

            function GetCourseName($courseList)
    {//how do I only emit the last 7 placements of the txt string?
        $courseName = substr(courseList, ..., ...)
    }
4

2 回答 2

1

PHP 文档substr涵盖了您需要了解的有关其行为的所有信息:

function GetCourseName($courseList)
{
    $courseName = substr($courseList, 0, -7);
}

您的应用程序的该文档的相关部分是:

如果给定长度(第三个参数)并且为负数,则从字符串末尾省略许多字符(在开始位置为负数时计算开始位置之后)。

于 2012-09-26T18:50:02.510 回答
0

首先是正确的,第二次使用这个 $courseName = substr($courseList, 0, strlen($courseList)-7); 见这里http://us.php.net/manual/en/function.substr.php

于 2012-09-26T18:54:05.620 回答