我正在使用 php 从数据库中提取信息,但我遇到了这个问题的绊脚石:
我有一个extra
列,其中单元格包含一些不同的值,布局如 keyword='lots of data'
。这是单元格内容的示例:
eTestOne='This would be one sentence.
This would be another.'
eTestTwo='1 Test
1 Other Test
2 Other Tests'
eTestThree='This would be another entry'
所以基本上我需要找到关键字 eTestTwo
并从“引号”中提取信息,保留换行符并将其放入单独的 <li>
中标签。
我想我已经走了一半了。这是我到目前为止的php:
$pos = stripos($info['extra'], "eTestTwo='");
echo substr($info['extra'], $pos + 9 );
但这并没有去掉结束引号后的额外信息,也不能帮助我区分每一行以放入自己的 <li>
中:
1 Test
1 Other Test
2 Other Tests'
eTestThree='This would be another entry'
我想要实现的最终输出是:
<li>1 Test</li>
<li>1 Other Test</li>
<li>2 Other Tests</li>
编辑:只是为了澄清,单元格可以包含更多或更少的值,没有特定的顺序。所以解决办法确实需要依赖找到关键字eTestTwo
并以某种方式从其引号之间获取信息。
最佳答案
编辑2:
$parts = explode("eTestTwo" , $info['extra']);
$temp = explode("'" ,$parts[1]);
/*
temp[0] = "="
temp[1] = "1 Test
1 Other Test
2 Other Tests"
temp[2] = "...."
*/
$lines = explode("\r" , $temp[1]);
unset($temp);
foreach($lines as $line)
echo "<li>".$line."</li>";
旧答案:
$parts = explode("=" , $info['extra']);
/*
parts[0] = eTestOne
parts[1] = 'This would be one sentence.
This would be another.'
parts[2] = eTestTwo
parts[3] = '1 Test
1 Other Test
2 Other Tests'
...
*/
$lines = explode("\n" , $parts[3]);
foreach($lines as $line)
echo "<li>".$line."</li>";
//It will work only if there are no '=' chars in the quotes.
关于php - 在数据库单元格中查找关键字以从 'quotes' 中提取信息,并保持换行符以放入单独的 <li> 标记中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9447206/