php爆炸选择多选

发布于 2024-11-18 02:01:54 字数 1569 浏览 2 评论 0原文

我想将这些 html 分为几个部分。一个

以及一些

作为一部分。我尝试了explode array('

','

'),但它导致了警告explode 不支持多选。

那么如何才能做到完美呢?谢谢。

$text=<<<EOT
<h2>title1</h2>
<p>something</p>
<span>something</span>
<h3>title2</h3>
<p>something</p>
<p>something</p>
<p>something</p>
<h2>title3</h2>
<span>something</span>
<h2>title4</h2>
<span>something</span>
<p>something</p>
<p>something</p>
EOT;

foreach ($text as $result) { 
    $arr = explode(array('<h2>','<h3>'),$result);
    reset($arr); 
    foreach($arr as $line){
        echo $line.'<hr />';
    } 
}

警告:第 23 行为 foreach() 提供的参数无效;

我的预期输出是:

<h2>title1</h2>
<p>something</p>
<span>something</span>
___________________________
<h3>title2</h3>
<p>something</p>
<p>something</p>
<p>something</p>
___________________________
<h2>title3</h2>
<span>something</span>
___________________________
<h2>title4</h2>
<span>something</span>
<p>something</p>
<p>something</p>
___________________________

I want divide these html as serval several part. One <h2> or <h3> with some <p> and <span> as one part. I tried explode array('<h2>','<h3>'), but it caused Warning. the explode not support multi choose.

So how to do it perfectly? Thanks.

$text=<<<EOT
<h2>title1</h2>
<p>something</p>
<span>something</span>
<h3>title2</h3>
<p>something</p>
<p>something</p>
<p>something</p>
<h2>title3</h2>
<span>something</span>
<h2>title4</h2>
<span>something</span>
<p>something</p>
<p>something</p>
EOT;

foreach ($text as $result) { 
    $arr = explode(array('<h2>','<h3>'),$result);
    reset($arr); 
    foreach($arr as $line){
        echo $line.'<hr />';
    } 
}

Warning: Invalid argument supplied for foreach() on line 23;

My expected output is:

<h2>title1</h2>
<p>something</p>
<span>something</span>
___________________________
<h3>title2</h3>
<p>something</p>
<p>something</p>
<p>something</p>
___________________________
<h2>title3</h2>
<span>something</span>
___________________________
<h2>title4</h2>
<span>something</span>
<p>something</p>
<p>something</p>
___________________________

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

对风讲故事 2024-11-25 02:01:54

您可以使用 preg_split()在不同的事情上爆炸。您可以在此处使用正则表达式:

$text = <<<EOT
<h2>title1</h2>
<p>something</p>
...
EOT;

$arr = preg_split("#(?=<h[23]>)#", $text);

if(isset($arr[0]) && trim($arr[0])=='') array_shift($arr); // remove first block if empty

foreach($arr as $block){
    echo $block."<hr />\n";
}

You can use preg_split() to explode at different things. You can use RegEx here:

$text = <<<EOT
<h2>title1</h2>
<p>something</p>
...
EOT;

$arr = preg_split("#(?=<h[23]>)#", $text);

if(isset($arr[0]) && trim($arr[0])=='') array_shift($arr); // remove first block if empty

foreach($arr as $block){
    echo $block."<hr />\n";
}
沙沙粒小 2024-11-25 02:01:54

您应该使用解析器来完成此类任务。我使用 Zend Framework,它有一个 解析器组件。否则,您可以使用普通的 PHP DOMElement。然后你可以使用 xpath 或 css 选择器查询你的 dom。示例:

<?php

$text=<<<EOT
<h2>title1</h2>
<p>something</p>
<span>something</span>
<h3>title2</h3>
<p>something</p>
<p>something</p>
<p>something</p>
<h2>title3</h2>
<span>something</span>
<h2>title4</h2>
<span>something</span>
<p>something</p>
<p>something</p>
EOT;


require_once 'Zend/Dom/Query.php';

$dom = new Zend_Dom_Query($text);
$results = $dom->query('h2');

foreach ($results as $domEl) {
    var_dump($domEl->nodeValue);
}
// outputs:
// string(6) "title1"
// string(6) "title3"
// string(6) "title4"

编辑:鉴于您的预期输出,我的示例并不完全符合您的需求,但您仍然需要一个解析器来执行这种 HTML 操作,因为解析器将 HTML 拆分为元素,并且您可以将它们作为标记来操作,而不是作为文本。

You should use a parser for this kind of tasks. I use Zend Framework which has a parser component. Otherwise you can use plain PHP DOMElement. Then you can query your dom with xpath or css selectors. Example:

<?php

$text=<<<EOT
<h2>title1</h2>
<p>something</p>
<span>something</span>
<h3>title2</h3>
<p>something</p>
<p>something</p>
<p>something</p>
<h2>title3</h2>
<span>something</span>
<h2>title4</h2>
<span>something</span>
<p>something</p>
<p>something</p>
EOT;


require_once 'Zend/Dom/Query.php';

$dom = new Zend_Dom_Query($text);
$results = $dom->query('h2');

foreach ($results as $domEl) {
    var_dump($domEl->nodeValue);
}
// outputs:
// string(6) "title1"
// string(6) "title3"
// string(6) "title4"

Edit: Given your expected output, my example doesn't fit exactly your needs, but you still need a parser to do that kind of HTML manipulation, because the parser splits the HTML in elements and you can manipulate them as tokens, not as text.

会傲 2024-11-25 02:01:54

好的,首先,警告发送到 foreach ,而不是爆炸。您正在尝试循环一个字符串(在本例中为$text)而不是一个数组

其次,即使 $text 是数组类型并且 $result 是字符串类型,您也尝试使用 array 作为分隔符在 explode() 调用中,但该函数希望第一个参数为输入字符串

我建议您查看How to parse HTML with PHP?搜索此术语,找到许多关于如何使用 PHP 解析 HTML 的帖子。

Ok, first, the warning is addressed to the foreach, not the explode. You are trying to loop a string ($text in this case) instead of an array.

Second, even if $text would be of type array and $result would be of type string, you are trying to use an array as delimiter in the explode() call, but that function wants the 1st parameter to be of type string.

I'd recommend to have a look at How to parse HTML with PHP? or to search SO for this terms, to find many many posts dealing with how to parse HTML with PHP.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文