PHP preg_match指定数字,并通过特定数字的区域获取节id
我想根据入口编号从本文中获取分区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
例如,当我输入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?(如果有任何正则表达式可以做到这一点,我会留下深刻印象,但是嘿-正则表达式太棒了!)非常感谢,我会试试。非常感谢,我会试试。