php vs regex for counting data
i have an array, it have same data:
data range
115X0101-115X0200
115X0101-115X0200
115X0101-115X0200
the 115x mean production code..this unimportant. we just concern at four digits behind it that we can counting.
1. i want script read or search "0101" and "0200"开发者_运维问答 from 115X0101-115X0200
2. i have tried using regex to count them become 200-101=100
3. the "115X0101-115X0200" repeated until there are 20 data like this
4. after it reached 20, show result at page:
data range
100
If this is the raw data, the easiest way to extract it is probably using a regular expression, as you've mentioned.
You'll probably want something like this (in PHP):
# Get this from the database
$sql_results = array(
'115X0101-115X0200',
'115X0101-115X0200',
'115X0101-115X0200',
);
foreach($sql_results as $row)
{
preg_match_all('/\d{4}/', $row, $matches);
#200 #101
echo intval($matches[0][1]) - intval($matches[0][0]) + 1;
}
For each row, preg_match will find groups of 4 digits (\d{4}
) and place them in $matches
(use var_dump($matches)
to see what it looks like).
More on Regex
- Regular Expressions Cheatsheet
- Regular Expressions Help
SQL Limit
Side note: If you only want 20 results at a time, you'll want to SELECT * FROM table LIMIT 20
when you query the database. To get rows 31-50 you'd use LIMIT 30, 20
, which means offset by 30, then get 20 rows.
精彩评论