I've got the following data in a db
我在数据库中有以下数据
[508] [blah-blah-blah] Random Text
[510] [hello-hello-hello] More random text
[542] [stuff-stuff-stuff] Even more text
This is in quite a lot of db cells. The whole block of text is in a cell and each row of the text is separated by a carriage return.
这是在很多db单元中。整个文本块位于单元格中,文本的每一行由一个回车分隔。
Ideally i want the number in the first square bracket on each line into array values. The data i want to end up with is:
理想情况下,我希望每一行的第一个方括号中的数字为数组值。我最后想要得到的数据是:
array(508,510,542)
More importantly, i want to see how to efficiently get that first data structure into a array. i feel like there should be a simple efficient way to use it, but, aside from some really complex regex i cannot see how to do it :(
更重要的是,我希望看到如何有效地将第一个数据结构转换为数组。我觉得应该有一种简单有效的方法来使用它,但是,除了一些非常复杂的regex之外,我不知道如何使用它:
Any help would be magical!
任何帮助都是神奇的!
3 个解决方案
#1
3
In PHP you can match the numbers using lookarounds for the literal brackets using preg_match_all()
在PHP中,你可以使用preg_match_all()来匹配文字括号中的数字
<?php
$string = '[508] [blah-blah-blah] Random Text
[510] [hello-hello-hello] More random text
[542] [stuff-stuff-stuff] Even more text';
preg_match_all ('!(?<=\[)([0-9]+)(?=\])!',$string,$matches);
print_r($matches[0]);
?>
Output
输出
Array
(
[0] => 508
[1] => 510
[2] => 542
)
To handle all of the records from the database, you would do something like this:
要处理来自数据库的所有记录,您可以这样做:
$result = mysqli_query($sql);
$records = array();
while ($row = mysqli_fetch_array($result)){
preg_match_all ('!(?<=\[)([0-9]+)(?=\])!',$row['my_text_field'],$matches);
foreach($matches[0] as $value){
$records[]=$value;
}
}
print_r($records);
#2
1
Given that your numbers always appear at the start of each line, the expression is pretty simple:
考虑到你的数字总是出现在每一行的开头,这个表达式非常简单:
$input = <<<EOM
[508] [blah-blah-blah] Random Text
[510] [hello-hello-hello] More random text
[542] [stuff-stuff-stuff] Even more text
EOM;
preg_match_all('/^\[(\d+)\]/m', $input, $matches);
print_r($matches[1]);
I'm using the /m
modifier to enable multiline mode, causing ^
to match the start of each line.
我使用/ m修饰符,使多行模式,导致^匹配每一行的开始。
#3
0
From the DB directly:
直接从数据库:
mysqli_query("SELECT SUBSTRING_INDEX(SUBSTRING(your_column_name, 1), ']', 1) FROM tablename");
SUBSTRING(str, pos) works like substr() would in php in that it extract a slice of the original string.
SUBSTRING(str, pos)在php中就像substr()一样,它从原始字符串中提取出一个片段。
SUBSTRING(your_column_name, 1)
means copy "your_column_name" starting from index 1 to the end.
子字符串(your_column_name, 1)表示从索引1到末尾复制“your_column_name”。
[508] [blah-blah-blah] Random Text
Becomes
508] [blah-blah-blah] Random Text
SUBSTRING_INDEX(str, C, X) copy the entire string str until the Xth occurence of character C
SUBSTRING_INDEX(str, C, X)复制整个字符串str,直到第Xth字符C出现
SUBSTRING_INDEX(str, ']', 1)
means copy the the entire string until the first occurence of ]
SUBSTRING_INDEX(str, ']', 1)表示复制整个字符串,直到第一次出现]
508] [blah-blah-blah] Random Text
Becomes
就变成了
508
#1
3
In PHP you can match the numbers using lookarounds for the literal brackets using preg_match_all()
在PHP中,你可以使用preg_match_all()来匹配文字括号中的数字
<?php
$string = '[508] [blah-blah-blah] Random Text
[510] [hello-hello-hello] More random text
[542] [stuff-stuff-stuff] Even more text';
preg_match_all ('!(?<=\[)([0-9]+)(?=\])!',$string,$matches);
print_r($matches[0]);
?>
Output
输出
Array
(
[0] => 508
[1] => 510
[2] => 542
)
To handle all of the records from the database, you would do something like this:
要处理来自数据库的所有记录,您可以这样做:
$result = mysqli_query($sql);
$records = array();
while ($row = mysqli_fetch_array($result)){
preg_match_all ('!(?<=\[)([0-9]+)(?=\])!',$row['my_text_field'],$matches);
foreach($matches[0] as $value){
$records[]=$value;
}
}
print_r($records);
#2
1
Given that your numbers always appear at the start of each line, the expression is pretty simple:
考虑到你的数字总是出现在每一行的开头,这个表达式非常简单:
$input = <<<EOM
[508] [blah-blah-blah] Random Text
[510] [hello-hello-hello] More random text
[542] [stuff-stuff-stuff] Even more text
EOM;
preg_match_all('/^\[(\d+)\]/m', $input, $matches);
print_r($matches[1]);
I'm using the /m
modifier to enable multiline mode, causing ^
to match the start of each line.
我使用/ m修饰符,使多行模式,导致^匹配每一行的开始。
#3
0
From the DB directly:
直接从数据库:
mysqli_query("SELECT SUBSTRING_INDEX(SUBSTRING(your_column_name, 1), ']', 1) FROM tablename");
SUBSTRING(str, pos) works like substr() would in php in that it extract a slice of the original string.
SUBSTRING(str, pos)在php中就像substr()一样,它从原始字符串中提取出一个片段。
SUBSTRING(your_column_name, 1)
means copy "your_column_name" starting from index 1 to the end.
子字符串(your_column_name, 1)表示从索引1到末尾复制“your_column_name”。
[508] [blah-blah-blah] Random Text
Becomes
508] [blah-blah-blah] Random Text
SUBSTRING_INDEX(str, C, X) copy the entire string str until the Xth occurence of character C
SUBSTRING_INDEX(str, C, X)复制整个字符串str,直到第Xth字符C出现
SUBSTRING_INDEX(str, ']', 1)
means copy the the entire string until the first occurence of ]
SUBSTRING_INDEX(str, ']', 1)表示复制整个字符串,直到第一次出现]
508] [blah-blah-blah] Random Text
Becomes
就变成了
508