簡體   English   中英

使用 cfDecodeEmail() 解碼 email 使用 PHP 時抑制 getAttribute() 錯誤

[英]Suppress getAttribute() error when using cfDecodeEmail() to decode email using PHP

目的:解碼cloudflare email

問題:未捕獲的Uncaught Error: Call to a member function getAttribute() on null

聽起來就像使用 is_null() 一樣簡單,但它不起作用。 嘗試了 is_object() 但我仍然無法擺脫錯誤。

客觀的:

$field['value'] = cfDecodeEmail($a->getAttribute("data-cfemail")); 代碼返回我已分配給 ACF 字段的 email 地址。

所以現在任何沒有該值的字段都會在 getAttribute() 上返回上面提到的錯誤。

因此,如果這會讓您指向我想要實現的目標:

if (is_null()) {
 echo '';
} else {
  $field['value'] = cfDecodeEmail($a->getAttribute("data-cfemail"));
}

整個代碼:

add_filter('acf/load_field/name=application_email', function($field) {
    $howtoapply = get_field('howtoapply');
    if( get_post_meta($post->ID, 'application_email', true) == '' ) {

$apply_link = get_field('howtoapply');

$link = $apply_link;
libxml_use_internal_errors(true);
$dom = new DOMDocument();
@$dom->loadHTML($link);
$dom->loadHTML($link);
foreach ($dom->getElementsByTagName("a") as $a) {
 
}

function cfDecodeEmail($encodedString){
  $k = hexdec(substr($encodedString,0,2));
  for($i=2,$email='';$i<strlen($encodedString)-1;$i+=2){
    $email.=chr(hexdec(substr($encodedString,$i,2))^$k);
  }
  return $email;
}

    $field['value'] = cfDecodeEmail($a->getAttribute("data-cfemail"));
        
    }
    
    return $field; 
    
});

我不知道這段代碼缺少什么。

謝謝。

$field['value'] = cfDecodeEmail($a->getAttribute('data-cfemail'));

您必須檢查nullis_object上的 object $a ,它將像這樣完成。

$field['value'] = ( ! is_null( $a ) && is_object( $a ) ) ? cfDecodeEmail( $a->getAttribute( 'data-cfemail' ) ) : '';

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM