简体   繁体   English

检查div ID是否在PHP中存在

[英]Check if div ID exists in PHP

I have same php to generate HTML in 2 ocasions: 我有相同的php在2种情况下生成HTML:

  1. Generate a link with target="_new" ; 生成一个带有target="_new"的链接;
  2. Generate a link without target property. 生成没有目标属性的链接。

The only way that I have to differentiate both of them is to create the parent div as different ID (eg: <div id="new"> for the 1st, '' for the 2nd. 我必须区分它们的唯一方法是将父div创建为不同的ID(例如,第一个创建<div id="new"> ,第二个创建“''。

Is there any way to check if has some #new in html and them set target? 有什么方法可以检查html中是否有一些#new并且它们设置了目标?

Here's the way that I've tried so far: 到目前为止,这是我尝试过的方法:

<?php $new = $html->find("#new"); ?>
<a href="<?php echo $item->getPrimaryLink()->getUrl(); ?>" <?php if (is_object($new)): ?> target="_new" <?php else : ?> <?php endif; ?> >

If you are following HTMLDOMPARSER then you can follow: 如果您遵循HTMLDOMPARSER,则可以遵循:

$html = file_get_html('http://www.google.com/');
$is_new_exist=false;
if($html->find('div#new'))
$is_new_exist=true;

And now you can use that flag for your checking 现在您可以使用该标志进行检查

For further query please checkout HTMLDOMPARSER 如需进一步查询,请签出HTMLDOMPARSER

I would presume you would be able to use something like 我想您将能够使用类似

$new = $html->find("#new");
if ($new) {
    echo something
} else {
    echo something else
}

Based on the assumption you are using domdocument or a html parser. 基于假设,您正在使用domdocument或html解析器。

You would not use target however but rather do something like <a href="#new" or if it were on another page <a href="somepage.php#new" 但是,您将不使用target ,而是使用<a href="#new"或者如果它在另一页上<a href="somepage.php#new"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM