1

i use curl php to get content from a web page, and i want to get content id="audio_file_path"

code :

<div class="mdl-detail-song">
    <div class="title">
    <span href="javascript:void(0);" class="txt>title</span>
      <div class="txt-singer"></div>
     <input type="hidden" id="audio_file_path" value="example.mp3">
  • 1
    What does this have to do with PHP? It's not clear what you're asking or what you're trying to do. Please elaborate, including your attempt and in what way it's not working as expected. – David Jun 10 '21 at 13:00
  • Unless You are trying to parse html through PHP, You cannot get the content id using php. – Indra Kumar S Jun 10 '21 at 13:02
  • i use curl php to get content from a web page, and i want to get the content id = "audio_file_path" , which is the path of the .mp3 file – Tran Tuan Truong Jun 10 '21 at 13:06
  • 2
    PHP already has libraries for parsing HTML, have you researched / tried anything? – ADyson Jun 10 '21 at 13:37
  • @TranTuanTruong: If you have an HTML file in PHP (which is essentially one long HTML string value) that you want to parse, the terminology for the tool(s) you're looking for is an "HTML Parser" or "DOM Parser". There are likely more than one available for PHP that you can try out and check their documentation for examples on how to use them. – David Jun 10 '21 at 14:22
  • 3
    Does this answer your question? [How can I get a div content in php](https://stackoverflow.com/questions/6491598/how-can-i-get-a-div-content-in-php) – Hyruma92 Jun 11 '21 at 10:47

2 Answers2

-1

Since you already have a string (the curl result body) you can use PHPs XPath or DOMDocument classes to extract some attribute values.

<?php
$html = '<div class="mdl-detail-song">
    <div class="title">
    <span href="javascript:void(0);" class="txt>title</span>
      <div class="txt-singer"></div>
     <input type="hidden" id="audio_file_path" value="example.mp3">';

libxml_use_internal_errors(true);
$dom = new DOMDocument();
$dom->omitXmlDeclaration = true;
$dom->preserveWhiteSpace = false;
$dom->validateOnParse = false;
$dom->strictErrorChecking = false;
$dom->formatOutput = false;
$dom->loadHTML('<?xml encoding="utf-8" ?>'.$html);
libxml_clear_errors();
libxml_use_internal_errors(false);

// use DOMDocument
$input = $dom->getElementsByTagName('input')->item(0); // first input in HTML
echo $input->getAttribute('id'); // audio_file_path

// use XPath
$xpath = new DOMXPath($dom);
$input = $xpath->query('(//input[@id]/@id)[1]'); // attribute id of first input which has an id attribute
echo $input->item(0)->nodeValue; // audio_file_path

Note that the HTML your trying to parse isn't complete or simply broken (which is why I added some checks you wouldn't need when reading proper HTML). This way you can read and parse strict and non-strict or html5ish html without problems.

Erik Pöhler
  • 742
  • 2
  • 9
  • 22
-1

You can use regex to find target part of string in preg_match_all(). add your content to $str

preg_match_all("/id=\"(\d+)\"/", $str, $matches);
// $matches[1] is array contain target values
echo implode(',', $matches[1]);

Or you can use DOMDocument.

https://stackoverflow.com/a/40267082/2444807

Hamodea Net
  • 105
  • 1
  • 1
  • 10