• Sorular
  • Etiketler

Regex kullanarak PHP Blogspot.com dan BlogId Ayrıştırma

1 Cevap php

How can i get the blogid from a given blogspot.com url? I looked at the source code of the webpage from a blogspot.com it looks like this

<link rel="EditURI" type="application/rsd+xml" title="RSD" href="http://www.blogger.com/rsd.g?blogID=4899870735344410268" />

nasıl ben bu numarayı 4899870735344410268 almak için ayrıştırmak

1 Cevap

DOMDocument belgeyi ayrıştırmak ve daha sonra istenilen eleman almak için kendi yöntemlerini kullanmak.

Ben bu herhelde: never use regular expressions to parse an HTML document.

function getBlogId($url) {
  $ch = curl_init($url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  $page = curl_exec ($ch);
  curl_close($ch);

  $doc = new DOMDocument();
  @$doc->loadHTML($page);

  $links = $doc->getElementsByTagName('link');

  foreach($links as $link) {
    $rel = $link->attributes->getNamedItem('rel');

    if($rel && $rel->nodeValue == 'EditURI') {
      $href = $link->attributes->getNamedItem('href')->nodeValue;
      $query = parse_url($href, PHP_URL_QUERY);

      if($query) {
        $queryComp = array();
        parse_str($query, $queryComp);

        if($queryComp['blogID']) {
          return $queryComp['blogID'];
        }
      }
    }
  }

  return false;
}

Örnek kullanım:

$id = getBlogId('http://thehouseinmarrakesh.blogspot.com/');
echo $id; // 483911541311389592
0

etiketler

  • php
  • parsing
  • Sorular
  • Etiketler
Copyright © 2014

Powered by EvrenWeb Int. Media.