Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP preg_match指定数字,并通过特定数字的区域获取节id_Php - Fatal编程技术网

PHP preg_match指定数字,并通过特定数字的区域获取节id

PHP preg_match指定数字,并通过特定数字的区域获取节id,php,Php,我想根据入口编号从本文中获取分区id 句号 例如,当我输入8时,周期将为6-12,我将得到“section-2”,21将得到“section-4”等…假设你知道你在做什么,我会得到这样的函数: function find_section($html, $value) { static $pattern = '/<li id="section\\-(\\d+)" role="example" label="(\\d+) \- (\\d+)"(>)/'; $of

我想根据入口编号从本文中获取分区id 句号


  • 例如,当我输入8时,周期将为6-12,我将得到“section-2”,21将得到“section-4”等…

    假设你知道你在做什么,我会得到这样的函数:

    function find_section($html, $value)
    {
        static $pattern = '/<li id="section\\-(\\d+)" role="example" label="(\\d+) \- (\\d+)"(>)/';
        $offset = 0;
    
        while (preg_match($pattern, $html, $matches, PREG_OFFSET_CAPTURE, $offset))
        {
            $section_id = (int) $matches[1][0];
            $range_min = (int) $matches[2][0];
            $range_max = (int) $matches[3][0];
            $offset = $matches[4][1] + 1;
    
            if ($value >= $range_min && $value < $range_max)
            { return 'section-' . $section_id; }
        }
    
        return null;
    }
    
    <?php
    
    $text = '<li id="section-1" role="example" label="1 - 6">
    <li id="section-2" role="example" label="6 - 12">
    <li id="section-3" role="example" label="12 - 18">
    <li id="section-4" role="example" label="18 - 24">';
    
    $pattern = '<li id="section-([0-9]+)" role="example" label="([0-9]+) - ([0-9]+)">';
    
    function find_section($value) {
        global $text, $pattern;
    
        preg_match_all($pattern, $text, $results);
    
        $index = 0;
        foreach($results[3] as $max) {
            if ($value < $max) {
                break;
            }
            $index++;
        }
    
        return "section-{$results[1][$index]}   {$results[2][$index]} - {$results[3][$index]}\n";
    }
    
    echo find_section(6);  // section-2   6 - 12
    echo find_section(21); // section-4   18 - 24
    
    输出:

    section-2
    section-4
    

    (使用PHP5.5.15进行测试)

    假设您知道自己在做什么,我会选择这样的函数:

    function find_section($html, $value)
    {
        static $pattern = '/<li id="section\\-(\\d+)" role="example" label="(\\d+) \- (\\d+)"(>)/';
        $offset = 0;
    
        while (preg_match($pattern, $html, $matches, PREG_OFFSET_CAPTURE, $offset))
        {
            $section_id = (int) $matches[1][0];
            $range_min = (int) $matches[2][0];
            $range_max = (int) $matches[3][0];
            $offset = $matches[4][1] + 1;
    
            if ($value >= $range_min && $value < $range_max)
            { return 'section-' . $section_id; }
        }
    
        return null;
    }
    
    <?php
    
    $text = '<li id="section-1" role="example" label="1 - 6">
    <li id="section-2" role="example" label="6 - 12">
    <li id="section-3" role="example" label="12 - 18">
    <li id="section-4" role="example" label="18 - 24">';
    
    $pattern = '<li id="section-([0-9]+)" role="example" label="([0-9]+) - ([0-9]+)">';
    
    function find_section($value) {
        global $text, $pattern;
    
        preg_match_all($pattern, $text, $results);
    
        $index = 0;
        foreach($results[3] as $max) {
            if ($value < $max) {
                break;
            }
            $index++;
        }
    
        return "section-{$results[1][$index]}   {$results[2][$index]} - {$results[3][$index]}\n";
    }
    
    echo find_section(6);  // section-2   6 - 12
    echo find_section(21); // section-4   18 - 24
    
    输出:

    section-2
    section-4
    

    (使用PHP5.5.15进行测试)

    您可以尝试以下方法:

    function find_section($html, $value)
    {
        static $pattern = '/<li id="section\\-(\\d+)" role="example" label="(\\d+) \- (\\d+)"(>)/';
        $offset = 0;
    
        while (preg_match($pattern, $html, $matches, PREG_OFFSET_CAPTURE, $offset))
        {
            $section_id = (int) $matches[1][0];
            $range_min = (int) $matches[2][0];
            $range_max = (int) $matches[3][0];
            $offset = $matches[4][1] + 1;
    
            if ($value >= $range_min && $value < $range_max)
            { return 'section-' . $section_id; }
        }
    
        return null;
    }
    
    <?php
    
    $text = '<li id="section-1" role="example" label="1 - 6">
    <li id="section-2" role="example" label="6 - 12">
    <li id="section-3" role="example" label="12 - 18">
    <li id="section-4" role="example" label="18 - 24">';
    
    $pattern = '<li id="section-([0-9]+)" role="example" label="([0-9]+) - ([0-9]+)">';
    
    function find_section($value) {
        global $text, $pattern;
    
        preg_match_all($pattern, $text, $results);
    
        $index = 0;
        foreach($results[3] as $max) {
            if ($value < $max) {
                break;
            }
            $index++;
        }
    
        return "section-{$results[1][$index]}   {$results[2][$index]} - {$results[3][$index]}\n";
    }
    
    echo find_section(6);  // section-2   6 - 12
    echo find_section(21); // section-4   18 - 24
    

    您可以尝试以下方法:

    function find_section($html, $value)
    {
        static $pattern = '/<li id="section\\-(\\d+)" role="example" label="(\\d+) \- (\\d+)"(>)/';
        $offset = 0;
    
        while (preg_match($pattern, $html, $matches, PREG_OFFSET_CAPTURE, $offset))
        {
            $section_id = (int) $matches[1][0];
            $range_min = (int) $matches[2][0];
            $range_max = (int) $matches[3][0];
            $offset = $matches[4][1] + 1;
    
            if ($value >= $range_min && $value < $range_max)
            { return 'section-' . $section_id; }
        }
    
        return null;
    }
    
    <?php
    
    $text = '<li id="section-1" role="example" label="1 - 6">
    <li id="section-2" role="example" label="6 - 12">
    <li id="section-3" role="example" label="12 - 18">
    <li id="section-4" role="example" label="18 - 24">';
    
    $pattern = '<li id="section-([0-9]+)" role="example" label="([0-9]+) - ([0-9]+)">';
    
    function find_section($value) {
        global $text, $pattern;
    
        preg_match_all($pattern, $text, $results);
    
        $index = 0;
        foreach($results[3] as $max) {
            if ($value < $max) {
                break;
            }
            $index++;
        }
    
        return "section-{$results[1][$index]}   {$results[2][$index]} - {$results[3][$index]}\n";
    }
    
    echo find_section(6);  // section-2   6 - 12
    echo find_section(21); // section-4   18 - 24
    

    好悲伤。一定有更好的办法吗?首先,您希望如何在PHP中实现这一点?这个HTML是否在某个字符串中,我们将搜索/解析它?第二,HTML是整个页面,还是像这里一样只是li的相关部分?第三,你尝试过什么PHP?(如果有任何正则表达式可以做到这一点,我会印象深刻,但嘿,正则表达式太棒了!)好的。一定有更好的办法吗?首先,您希望如何在PHP中实现这一点?这个HTML是否在某个字符串中,我们将搜索/解析它?第二,HTML是整个页面,还是像这里一样只是li的相关部分?第三,你尝试过什么PHP?(如果有任何正则表达式可以做到这一点,我会留下深刻印象,但是嘿-正则表达式太棒了!)非常感谢,我会试试。非常感谢,我会试试。