foreach循环的问题
我编写了这段代码:
$matches = array();
preg_match_all('/"Type":".+?",/', $text, $matches);
foreach($matches[0] as $match) {
if (isset($_GET['dvd']) && !empty($_GET['dvd'])) {
$dvd = $_GET['dvd'];
if (stripos($match, 'DVD') !== false) {
$match = '';
}
}
echo $match;
}
在这段代码中,我在 $text 中搜索单词“type”,并将整行及其前面的单词存储在数组中。然后我循环浏览每一个,看看是否有 DVD 这个词。如果是,则将其删除并显示一个空行。
现在我还想搜索假设制造商并将其显示在返回的每种类型下方。
所以它应该返回假设结果:
Type: HDD
Manufacturer: WD
Type: Flash Drive
Manufacturer: Transcend
Type: CD
Manufacturer: Sony
所以我尝试放置另一个 preg_match_all 表达式:
$anotherMatch = array();
preg_match_all('/"Manufacturer":".+?",/', $text, $anotherMatch);
并且我尝试将其与前面的 foreach 表达式与 && 结合起来。运算符但它不起作用。我还尝试了不同的 foreach 表达式,然后尝试了一种用于最后回显的表达式。但这也不起作用。
你能告诉我如何达到想要的结果吗?谢谢...
I made this code :
$matches = array();
preg_match_all('/"Type":".+?",/', $text, $matches);
foreach($matches[0] as $match) {
if (isset($_GET['dvd']) && !empty($_GET['dvd'])) {
$dvd = $_GET['dvd'];
if (stripos($match, 'DVD') !== false) {
$match = '';
}
}
echo $match;
}
In this code i search for the word "type" in $text and it stores the whole line with the words ahead of it in the array. And then i loop through each one and see if the word DVD is there. If it is then, it removes it and displays a blank line.
Now I also want to search for suppose manufacturer and display it beneath every type returned.
So it should return suppose the result :
Type: HDD
Manufacturer: WD
Type: Flash Drive
Manufacturer: Transcend
Type: CD
Manufacturer: Sony
So I tried it with putting another preg_match_all expression:
$anotherMatch = array();
preg_match_all('/"Manufacturer":".+?",/', $text, $anotherMatch);
And I tried combining this with the previous foreach expression with an && operator but it didn't work. Also I tried different foreach expressions and then one for echoing at the end. but that also didn't work.
Can you tell me how to achieve the desired result. Thanks...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
给定像这样的源输入:
并期望像这样的输出:
您可以使用这个正则表达式:
并像这样循环它:
虽然,我认为这不会完全按照您想要的方式进行。
Given source input like this:
And expecting output like this:
You could use this regular expression:
And loop over it like this:
Although, I don't think that will do quite exactly what you want it to.