帮助循环查找具有键和值的数组
我正在尝试创建一个从 html 中提取文本的函数,用于多个步骤,但我不知道如何形成一个循环。
这就是任务。我有这个数组
$arr = array("<div>" => "</div>", "<p>" => "</p>", "<h3>" => "</h3>");
现有的工作函数 cut($a, $b, $c)
在这种情况下获取中间的内容 $a = "
。
我想做的是做到这一点:
- 步骤 1 - 从 div 剪切到 div
- 步骤 2 - foreach 结果来自步骤 1, 从 p 到 p
- 第 3 步 - foreach 来自第 2 步的结果, 从 h3 剪切到 h3
- 步骤 n,其中 n 是数组长度。
虽然我知道我可以使用 foreach 来获取结果并应用第二步,但我不能概括这一点。
编辑
我有一个名为 cut
的现有函数,如上所述。我想创建一个名为 cutlayer($arr, $html)
的新函数,其中 $arr 是上面的 arr 。我需要 cutlayer 函数来使用剪切函数并执行上面提到的以下步骤,但我不知道如何做到这一点。
谢谢
I am trying to make this function that extracts text from html for multiple steps but I cant figure out how to make a loop form it.
This is the task. I have this array
$arr = array("<div>" => "</div>", "<p>" => "</p>", "<h3>" => "</h3>");
The existing working function cut($a, $b, $c)
that gets the content in between in this case $a = "<div>", $b="</div>" and $c = the html
.
What i am trying to do is to make this:
- Step 1 - cut from div to div
- Step 2 - foreach results from step1,
cut from p to p - Step 3 - foreach results from step2,
cut from h3 to h3 - Step n where n is array length.
Although I know that I can use foreach to get the results and apply step two, I cant generalize this.
EDIT
I have an existing function called cut
that has been described above. I want to create a new function called cutlayer($arr, $html)
where $arr is arr from above. I need the cutlayer function to use the cut function and do the following steps mentioned above but I cant figure out how to do that.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为了省去麻烦,请使用专为解析 HTML 设计的工具包。 PHP 的
DOMDocument
就是为这些任务而设计的。除非这是家庭作业并且您被指示使用数组匹配方法......
Save yourself the trouble and use a toolkit designed for parsing HTML. PHP's
DOMDocument
is made for these tasks.Unless this is homework and you were instructed to use your array matching method....