我已经把头发拉了好几个小时了。我试图找到iframe
的所有位置。第一次出现是成功但不是第二次出现。
这是我的代码
$ofAllIFrames = qp( $this->content, 'iframe' );
$iframes = array();
$allIframes = array();
$startTag = 0;
foreach( $ofAllIFrames as $iframe) {
$startCurrentTag = strpos( $this->content, '<iframe>', $startTag );
$endCurrentTag = strpos( $this->content, '</iframe>', $startTag );
$iframes[] = array(
'start' => $startCurrentTag,
'end' => $endCurrentTag
);
$allIframes[] = $iframe;
$startTag = $endCurrentTag;
var_dump($startTag);
ob_flush();
}
return array(
'hasIFrame' => count( $allIframes ) > 0,
'elements' => $iframes
);
这是我的测试用例
public function test_if_content_has_multiple_iframes() {
$content = 'some content <iframe></iframe> <iframe id="1"></iframe> and another content';
$iframeChecker = new IFrame_Checker( $content );
$params = $iframeChecker->check();
$this->assertTrue( $params['hasIFrame'] );
$this->assertEquals( 13, $params['elements'][0]['start'] );
$this->assertEquals( 21, $params['elements'][0]['end'] );
$this->assertEquals( 31, $params['elements'][1]['start'] );
$this->assertEquals( 46, $params['elements'][1]['end'] );
}
第二个iframe的测试失败
1) Test_IFrame::test_if_content_has_multiple_iframes
Failed asserting that false matches expected 31.
答案 0 :(得分:1)
$startCurrentTag = strpos( $this->content, '<iframe>', $startTag );
$endCurrentTag = strpos( $this->content, '</iframe>', $startCurrentTag );
应该更好。
原因:
$ startTag 等于前一个循环中的 $ endCurrentTag ,它引用</iframe>...
处的偏移量。因此,如果您使用$endCurrentTag = strpos( $this->content, '</iframe>', $startTag );