Php 检查属性hreflang是否存在
我想检查hreflang属性是否存在,我不确定最好的方法是什么?Php 检查属性hreflang是否存在,php,attr,hreflang,Php,Attr,Hreflang,我想检查hreflang属性是否存在,我不确定最好的方法是什么?preg\u match是唯一的方法还是更多的方法 function add_hreflang() { if( !($attribute['hreflang']) ){ // do something } } add_action('wp_head', 'add_hreflang', 1); 您可以使用!空()检查 function add_hreflang() { if( !empty($at
preg\u match
是唯一的方法还是更多的方法
function add_hreflang() {
if( !($attribute['hreflang']) ){
// do something
}
}
add_action('wp_head', 'add_hreflang', 1);
您可以使用!空()
检查
function add_hreflang() {
if( !empty($attribute['hreflang']) ){
// do something
}
}
编辑:
要检查是否存在任何特定属性,您可以使用以下代码:它将html元素转换为xml对象,然后将xml转换为数组,并找出数组中是否存在所需的属性。链接到原始文件和代码
$str='';
$link=新的simplexmlement($str);
//打印(链接);
函数xml2array($xmlObject,$out=array()){
foreach((数组)$xmlObject作为$index=>$node)
$out[$index]=(is_对象($node))?xml2array($node):$node;
退回$out;
}
$arr=xml2array($link);
打印($arr['@attributes']);
我已经测试过了,但它是否也会检查
是否确实存在?@Demian请查看编辑后的答案。好吧,这看起来很复杂,但这只检查特定的字符串?它不能仅仅检查DOM中是否存在属性为hreflang
的
元素?我认为这可以通过java脚本或jquery实现。但使用php,您不能直接与DOM交互,因为它是一种服务器端语言,不能在浏览器中运行
$str = '<a href="http://example.com" anotherAttrib="someValue">Link</a>';
$link = new SimpleXMLElement($str);
// print_r($link);
function xml2array( $xmlObject, $out = array () ){
foreach ( (array) $xmlObject as $index => $node )
$out[$index] = ( is_object ( $node ) ) ? xml2array ( $node ) : $node;
return $out;
}
$arr = xml2array($link);
print_r($arr['@attributes']);