English 中文(简体)
PHP DOM Check
原标题:PHP DOM Check if img inside a div
  • 时间:2011-10-15 03:38:47
  •  标签:
  • php
  • dom

我正试图在某个地点做一些动态的CSS风格更新,并且需要在贾瓦文中使用PHP。 我只需要带带<条码>div的标签,标有<条码>img 标签,忽略了<条码>div其余部分。

Here is what I have so far:


    $dom = new domDocument;
    $dom->loadHTML($test);
    $dom->preserveWhiteSpace = false;

    $divs = $dom->getElementsByTagName( div );
    foreach ($divs as $div) {
        $saveXML[] = $dom->saveXML($div);
        $childNotes[] = $div->getChildNodes;
    }

我只是在这里尝试两种方法。 <代码>$saveXML备选办法的问题是,它给我整个编码,包括列载div。 我无意这样做。 从现在起,我将不得不在法典上操作一个或一个部分,以发现它有<条码>mg。

The $childNodes option should have been the best I thought but the array is returned empty. It would have been nice because I could have simply tried something like


    if ($childNode->nodeType ==  img ){
      $div->setAttribute( class , $new_attributes);
    }

(我坚决认为,最后一点是完全的,而是一般的概念。)

这里是需要通过超文本检索的样本。


    div class="image-left drop-shadow raised" style="width: 210px">
         img src="images/myimage.jpg" width="200" height="150" class="size-full" /
         p>EDIT THIS DIV s STYLE /p>
    /div>
    div class="blocktext" style="width: 150px;">
         p>DONT EDIT THIS DIVs STYLE /p>
    /div>

Any help please...

问题回答

我希望您的样本html能够使用实际的html。 很多ur子用露天/封闭式“和”;

然而,如果没有,你可以使用:

$childNode->getElementsByTagName( img ); 

如果有图像,从http://php.net/manual/en/domdocument.getelementsbytagname.php“rel=“nofollow”http://php.net/manual/en/domdocument.getelementsbytag.php i. 回到名单...... 我可能错了,但强行把思想带走。

http://php.net/domxpath.query“rel=“nofollow” Xpath query:

$xp = new DOMXPath($dom)
$divsContainingImg = $xp->query( //div/img/.. );
foreach ($divsContainingImg as $div)
{
    /* @var $div DOMElement */
    ...
}

这将选择所有<代码>img的内容,即direct<>m> children of a div elements,然后将其括号(d<>d.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签