[PHP] ajout dans l'attribut href

Tags: #<Tag:0x00007f509f75c078>

Bonjour, bonjour…

Sur mon site perso, je collecte quelques flux RSS/Atom, grâce à la function ‘simple_load_xml’ de PHP, et entres autres le flux RSS de mon compte github.
Hors, je me rend compte que les urls renseignées dans l’attribut href sont de type relatif, et non absolu… il manque tout simplement le scheme et le nom de domaine de github avant… (tel que : https://github.com)

Le contenu est celui de $item->content, et peut être de cet acabit :

<div class="issues_comment"><div class="body">
<!-- issue_comment -->
<div class="d-flex border-bottom border-gray-light py-3">
    <span class="mr-3">
  <a data-hovercard-user-id="3951851" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" data-hydro-click="{&quot;event_type&quot;:&quot;news_feed.event.click&quot;,&quot;payload&quot;:{&quot;event&quot;:{&quot;repo_id&quot;:2597648,&quot;actor_id&quot;:3951851,&quot;public&quot;:true,&quot;type&quot;:&quot;IssueCommentEvent&quot;,&quot;target_id&quot;:null,&quot;id&quot;:7536739750,&quot;additional_details_shown&quot;:false,&quot;grouped&quot;:false},&quot;event_group&quot;:null,&quot;org_id&quot;:null,&quot;target_type&quot;:&quot;event&quot;,&quot;action_target&quot;:&quot;actor&quot;,&quot;client_id&quot;:null,&quot;user_id&quot;:null,&quot;originating_request_id&quot;:&quot;95D8:105C:3AA8D75:68286EB:5AD4EC2D&quot;}}" data-hydro-click-hmac="166971e9a010a274823688b7e42885ab7ff197d261573cddce61393130ae374b" data-ga-click="News feed, event click, Event click type:IssueCommentEvent target:actor" href="/hucste" rel="noreferrer"><img class="avatar" src="https://avatars0.githubusercontent.com/u/3951851?s=64&amp;v=4" width="32" height="32" alt="@hucste" data-hovercard-user-id="3951851"></a>
</span>

  <div class="d-flex flex-column width-full">
      <div class="d-flex flex-justify-between flex-items-baseline mb-2">
        <div>
          <a data-hovercard-user-id="3951851" data-octo-click="hovercard-link-click" data-octo-dimensions="link_type:self" data-hydro-click="{&quot;event_type&quot;:&quot;news_feed.event.click&quot;,&quot;payload&quot;:{&quot;event&quot;:{&quot;repo_id&quot;:2597648,&quot;actor_id&quot;:3951851,&quot;public&quot;:true,&quot;type&quot;:&quot;IssueCommentEvent&quot;,&quot;target_id&quot;:null,&quot;id&quot;:7536739750,&quot;additional_details_shown&quot;:false,&quot;grouped&quot;:false},&quot;event_group&quot;:null,&quot;org_id&quot;:null,&quot;target_type&quot;:&quot;event&quot;,&quot;action_target&quot;:&quot;actor&quot;,&quot;client_id&quot;:null,&quot;user_id&quot;:null,&quot;originating_request_id&quot;:&quot;95D8:105C:3AA8D75:68286EB:5AD4EC2D&quot;}}" data-hydro-click-hmac="166971e9a010a274823688b7e42885ab7ff197d261573cddce61393130ae374b" data-ga-click="News feed, event click, Event click type:IssueCommentEvent target:actor" class="link-gray-dark no-underline text-bold wb-break-all" href="/hucste" rel="noreferrer">hucste</a>
          
          commented on
          issue
          <a title="Cant install on OpenBSD 6.x" class="link-gray-dark text-bold" data-hydro-click="{&quot;event_type&quot;:&quot;news_feed.event.click&quot;,&quot;payload&quot;:{&quot;event&quot;:{&quot;repo_id&quot;:2597648,&quot;actor_id&quot;:3951851,&quot;public&quot;:true,&quot;type&quot;:&quot;IssueCommentEvent&quot;,&quot;target_id&quot;:null,&quot;id&quot;:7536739750,&quot;additional_details_shown&quot;:false,&quot;grouped&quot;:false},&quot;event_group&quot;:null,&quot;org_id&quot;:null,&quot;target_type&quot;:&quot;event&quot;,&quot;action_target&quot;:&quot;issue-comment&quot;,&quot;client_id&quot;:null,&quot;user_id&quot;:null,&quot;originating_request_id&quot;:&quot;95D8:105C:3AA8D75:68286EB:5AD4EC2D&quot;}}" data-hydro-click-hmac="8017af519c829077ba7114e429209deb9cb8b5e7869b5acd94a8f140104f66d5" data-ga-click="News feed, event click, Event click type:IssueCommentEvent target:issue-comment" href="/geany/geany-plugins/issues/734#issuecomment-381636142" rel="noreferrer">geany/geany-plugins#734</a>
        </div>
        <span class="f6 text-gray-light ml-4">
          <relative-time datetime="2018-04-16T15:04:42Z">Apr 16, 2018</relative-time>
        </span>
      </div>
    <div class="message markdown-body Box border-gray-light p-3">
      <div class="f6 mb-1">
        <a title="Cant install on OpenBSD 6.x" class="link-gray" data-hydro-click="{&quot;event_type&quot;:&quot;news_feed.event.click&quot;,&quot;payload&quot;:{&quot;event&quot;:{&quot;repo_id&quot;:2597648,&quot;actor_id&quot;:3951851,&quot;public&quot;:true,&quot;type&quot;:&quot;IssueCommentEvent&quot;,&quot;target_id&quot;:null,&quot;id&quot;:7536739750,&quot;additional_details_shown&quot;:false,&quot;grouped&quot;:false},&quot;event_group&quot;:null,&quot;org_id&quot;:null,&quot;target_type&quot;:&quot;event&quot;,&quot;action_target&quot;:&quot;issue-comment&quot;,&quot;client_id&quot;:null,&quot;user_id&quot;:null,&quot;originating_request_id&quot;:&quot;95D8:105C:3AA8D75:68286EB:5AD4EC2D&quot;}}" data-hydro-click-hmac="8017af519c829077ba7114e429209deb9cb8b5e7869b5acd94a8f140104f66d5" data-ga-click="News feed, event click, Event click type:IssueCommentEvent target:issue-comment" href="/geany/geany-plugins/issues/734#issuecomment-381636142" rel="noreferrer"><img class="avatar mr-1" src="https://avatars3.githubusercontent.com/u/3951851?s=32&amp;v=4" width="16" height="16" alt="@hucste" data-hovercard-user-id="3951851"> <span class="link-gray-dark text-bold">hucste</span> commented <relative-time datetime="2018-04-16T15:04:42Z">Apr 16, 2018</relative-time></a>
      </div>
        <p>But, for OpenBSD 6.2:
<code>$ clang++ -v
OpenBSD clang version 4.0.0 (tags/RELEASE_400/final) (based on LLVM 4.0.0)
Target: amd64-unknown-openbsd6.2
Thre…</code></p>
    </div>
  </div>
</div>
</div></div>

Bref, quel est le “meilleur” moyen, s’il en est, de parcourir le code html retourné par $item->content, pour ajouter le sheme + le nom de domaine dans l’attribut href de l’élément a, s’il n’existe pas dans cet attribut ?

^^ en JS !?

Dsl, mais insatifaisant…
Le traitement est purement en PHP, je ne vais pas rajouter une surcouche…

Je pense me diriger vers la manipulation du DOM, mais j’hésite - parce que je ne maîtrise pas ce genre de pratique :stuck_out_tongue:

Bon, je l’ai résolu ainsi - semble-t-il :

$dom = new DOMDocument();
                @$dom->loadHTML($item->content);
                    
                foreach ($dom->getElementsByTagName('a') as $a) {
                    
                    $urls = parse_url($a->getAttribute('href'));
                    if(array_key_exists('scheme', $urls) == False) $urls['scheme'] = 'https';
                    if(array_key_exists('host', $urls) == False) $urls['host'] = $domain;
                        
                    $a->setAttribute('href', build_url($urls));
                        
                }
                unset($a);

            $content = $dom->saveHTML();

Et la function buil_urls() est :

function build_url(array $parts) {
    return (isset($parts['scheme']) ? "{$parts['scheme']}:" : '') . 
        ((isset($parts['user']) || isset($parts['host'])) ? '//' : '') . 
        (isset($parts['user']) ? "{$parts['user']}" : '') . 
        (isset($parts['pass']) ? ":{$parts['pass']}" : '') . 
        (isset($parts['user']) ? '@' : '') . 
        (isset($parts['host']) ? "{$parts['host']}" : '') . 
        (isset($parts['port']) ? ":{$parts['port']}" : '') . 
        (isset($parts['path']) ? "{$parts['path']}" : '') . 
        (isset($parts['query']) ? "?{$parts['query']}" : '') . 
        (isset($parts['fragment']) ? "#{$parts['fragment']}" : '');
}

Bien sûr, j’appelle non plus $item->content pour afficher le contenu modifié, mais bel et bien $content :wink:

1 J'aime

@PengouinPdt :slight_smile:

1 J'aime

Je note la méthode ça pourrait me servir plus tard :wink:

2 J'aime