检查div ID是否在PHP中存在 [英] Check if div ID exists in PHP

查看:69
本文介绍了检查div ID是否在PHP中存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有相同的php可以在2种情况下生成HTML:

I have same php to generate HTML in 2 ocasions:

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

我唯一要区分它们的唯一方法是将父div创建为不同的ID(例如:<div id="new">表示第一个,''表示第二个.

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.

有什么方法可以检查html中是否有#new并且它们设置了目标?

Is there any way to check if has some #new in html and them set target?

这是到目前为止我尝试过的方法:

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; ?> >

推荐答案

如果您关注的是 HTMLDOMPARSER ,那么您可以遵循:

If you are following HTMLDOMPARSER then you can follow:

$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

进一步查询,请结帐 HTMLDOMPARSER

这篇关于检查div ID是否在PHP中存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆