Getting a single PHP parse value

Question:

$ch = curl_init('site');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/6.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.7) Gecko/20050414 Firefox/1.0.3");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_COOKIEJAR, 'x.txt');
curl_setopt($ch, CURLOPT_COOKIEFILE, 'x.txt');
$html = curl_exec($ch);
$dom = new DOMDocument();

@$dom->loadHTML($html);

$link = array();
foreach($dom->getElementsByTagName('a') as $link) {
  # Mostrar todos os elemento que estiver dentro da tag href


$x = $link->getAttribute('href'); // exibi todos os conteudo dentro do href !
// pegar apenas o link completo
#https://www.site.com/checkout.asp?ref=fvFDGND2MYQ

///
echo $x;

poem this way I can get all the links from the html received from cURL

http://prntscr.com/5icnbf

and for me to just get the https://www.site.com/checkout.asp?ref=fvFDGND2MYQ ? just this link?

Answer:

If you want a particular link , you can check its content to find out if it's actually what you want:

// ...
foreach ($dom->getElementsByTagName('a') as $link) {

  /* Verifica se é o link que eu quero
   */
  $href = $link->getAttribute('href');
  if (strpos($href, "checkout.asp?ref=") !== FALSE) {
    $x = $href;  // guarda o link
    break;       // sair do ciclo
  }
}

To check, what was done was to check if the expression checkout.asp?ref= exists in the link to be processed, using the strpos() function. You should change the expression if you are looking for a link other than the one in the question.

You can see an example working on Ideone combined with your previous problem.

Scroll to Top