Phân tích cú pháp HTML cho phép chúng tôi chuyển đổi nội dung hoặc đánh dấu của nó thành chuỗi, giúp phân tích hoặc tạo tệp HTML động dễ dàng hơn. Chi tiết hơn, nó lấy mã HTML thô, đọc nó, tạo cấu trúc đối tượng cây DOM từ các đoạn văn đến tiêu đề và cho phép chúng tôi trích xuất thông tin quan trọng hoặc cần thiết
Chúng tôi phân tích các tệp HTML bằng thư viện tích hợp và đôi khi là thư viện của bên thứ ba để quét web hoặc phân tích nội dung trong PHP. Tùy thuộc vào phương pháp, mục tiêu là chuyển đổi nội dung tài liệu HTML thành một chuỗi để trích xuất từng thẻ HTML
Bài viết này sẽ thảo luận về lớp dựng sẵn,
loadHTML[$html];
$dom->preserveWhiteSpace = false;
$content = $dom->getElementsByTagName[$element];
foreach [$content as $each] {
echo $each->nodeValue;
echo "\n";
}
}
echo "The H2 contents are:\n";
getRootElement["h2", $html];
echo "\n";
echo "The H3 contents are:\n";
getRootElement["h3", $html];
echo "\n";
echo "The Paragraph contents include\n";
getRootElement["p", $html];
echo "\n";
1, và hai thư viện bên thứ ba, loadHTML[$html];
$dom->preserveWhiteSpace = false;
$content = $dom->getElementsByTagName[$element];
foreach [$content as $each] {
echo $each->nodeValue;
echo "\n";
}
}
echo "The H2 contents are:\n";
getRootElement["h2", $html];
echo "\n";
echo "The H3 contents are:\n";
getRootElement["h3", $html];
echo "\n";
echo "The Paragraph contents include\n";
getRootElement["p", $html];
echo "\n";
2 và loadHTML[$html];
$dom->preserveWhiteSpace = false;
$content = $dom->getElementsByTagName[$element];
foreach [$content as $each] {
echo $each->nodeValue;
echo "\n";
}
}
echo "The H2 contents are:\n";
getRootElement["h2", $html];
echo "\n";
echo "The H3 contents are:\n";
getRootElement["h3", $html];
echo "\n";
echo "The Paragraph contents include\n";
getRootElement["p", $html];
echo "\n";
0Sử dụng loadHTML[$html];
$dom->preserveWhiteSpace = false;
$content = $dom->getElementsByTagName[$element];
foreach [$content as $each] {
echo $each->nodeValue;
echo "\n";
}
}
echo "The H2 contents are:\n";
getRootElement["h2", $html];
echo "\n";
echo "The H3 contents are:\n";
getRootElement["h3", $html];
echo "\n";
echo "The Paragraph contents include\n";
getRootElement["p", $html];
echo "\n";
1 để phân tích cú pháp HTML trong PHP
loadHTML[$html];
$dom->preserveWhiteSpace = false;
$content = $dom->getElementsByTagName[$element];
foreach [$content as $each] {
echo $each->nodeValue;
echo "\n";
}
}
echo "The H2 contents are:\n";
getRootElement["h2", $html];
echo "\n";
echo "The H3 contents are:\n";
getRootElement["h3", $html];
echo "\n";
echo "The Paragraph contents include\n";
getRootElement["p", $html];
echo "\n";
Cho dù là tệp HTML cục bộ hay trang web trực tuyến, các lớp
loadHTML[$html];
$dom->preserveWhiteSpace = false;
$content = $dom->getElementsByTagName[$element];
foreach [$content as $each] {
echo $each->nodeValue;
echo "\n";
}
}
echo "The H2 contents are:\n";
getRootElement["h2", $html];
echo "\n";
echo "The H3 contents are:\n";
getRootElement["h3", $html];
echo "\n";
echo "The Paragraph contents include\n";
getRootElement["p", $html];
echo "\n";
2 và loadHTML[$html];
$dom->preserveWhiteSpace = false;
$content = $dom->getElementsByTagName[$element];
foreach [$content as $each] {
echo $each->nodeValue;
echo "\n";
}
}
echo "The H2 contents are:\n";
getRootElement["h2", $html];
echo "\n";
echo "The H3 contents are:\n";
getRootElement["h3", $html];
echo "\n";
echo "The Paragraph contents include\n";
getRootElement["p", $html];
echo "\n";
3 giúp phân tích cú pháp tệp HTML và lưu trữ phần tử của nó dưới dạng chuỗi hoặc, trong trường hợp ví dụ của chúng tôi, một mảngHãy phân tích cú pháp tệp HTML này bằng các hàm và trả về tiêu đề, tiêu đề phụ và đoạn văn
Document
Welcome to the Abode of PHP
PHP has been the saving grace of the internet from its inception, it
runs over 70% of website on the internet
Understanding PHP
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Eum minus
eos cupiditate earum et optio culpa, eligendi facilis laborum
dolore.
Using PHP
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Eum minus
eos cupiditate earum et optio culpa, eligendi facilis laborum
dolore.
Install PHP
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Eum minus
eos cupiditate earum et optio culpa, eligendi facilis laborum
dolore.
Configure PHP
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Eum minus
eos cupiditate earum et optio culpa, eligendi facilis laborum
dolore.
Welcome to the Abode of JS
PHP has been the saving grace of the internet from its inception, it
runs over 70% of website on the internet
Understanding JS
Lorem ipsum dolor, sit amet consectetur adipisicing elit. Eum minus
eos cupiditate earum et optio culpa, eligendi facilis laborum
dolore.
mã PHP
loadHTML[$html];
$dom->preserveWhiteSpace = false;
$content = $dom->getElementsByTagName[$element];
foreach [$content as $each] {
echo $each->nodeValue;
echo "\n";
}
}
echo "The H2 contents are:\n";
getRootElement["h2", $html];
echo "\n";
echo "The H3 contents are:\n";
getRootElement["h3", $html];
echo "\n";
echo "The Paragraph contents include\n";
getRootElement["p", $html];
echo "\n";
Đầu ra của đoạn mã là
The H2 contents are:
Welcome to the Abode of PHP
Welcome to the Abode of JS
The H3 contents are:
Understanding PHP
Using PHP
Install PHP
Configure PHP
Understanding JS
The Paragraph contents include
PHP has been the saving grace of the internet from its inception, it
runs over 70% of the website on the internet
...
Sử dụng loadHTML[$html];
$dom->preserveWhiteSpace = false;
$content = $dom->getElementsByTagName[$element];
foreach [$content as $each] {
echo $each->nodeValue;
echo "\n";
}
}
echo "The H2 contents are:\n";
getRootElement["h2", $html];
echo "\n";
echo "The H3 contents are:\n";
getRootElement["h3", $html];
echo "\n";
echo "The Paragraph contents include\n";
getRootElement["p", $html];
echo "\n";
2 để phân tích cú pháp HTML trong PHP
loadHTML[$html];
$dom->preserveWhiteSpace = false;
$content = $dom->getElementsByTagName[$element];
foreach [$content as $each] {
echo $each->nodeValue;
echo "\n";
}
}
echo "The H2 contents are:\n";
getRootElement["h2", $html];
echo "\n";
echo "The H3 contents are:\n";
getRootElement["h3", $html];
echo "\n";
echo "The Paragraph contents include\n";
getRootElement["p", $html];
echo "\n";
Đối với các chức năng bổ sung, chẳng hạn như bộ chọn kiểu CSS, bạn có thể sử dụng thư viện của bên thứ ba có tên là Trình phân tích cú pháp DOM HTML đơn giản, đây là trình phân tích cú pháp PHP đơn giản và nhanh chóng. Bạn có thể tải xuống và bao gồm hoặc yêu cầu tệp PHP duy nhất
Với quy trình này, bạn có thể dễ dàng phân tích cú pháp qua tất cả các yếu tố bạn muốn. Sử dụng cùng một đoạn mã như trong phần trước, chúng ta sẽ phân tích cú pháp HTML bằng cách sử dụng hàm có tên là
loadHTML[$html];
$dom->preserveWhiteSpace = false;
$content = $dom->getElementsByTagName[$element];
foreach [$content as $each] {
echo $each->nodeValue;
echo "\n";
}
}
echo "The H2 contents are:\n";
getRootElement["h2", $html];
echo "\n";
echo "The H3 contents are:\n";
getRootElement["h3", $html];
echo "\n";
echo "The Paragraph contents include\n";
getRootElement["p", $html];
echo "\n";
5, hàm này xử lý HTML và sử dụng phương thức loadHTML[$html];
$dom->preserveWhiteSpace = false;
$content = $dom->getElementsByTagName[$element];
foreach [$content as $each] {
echo $each->nodeValue;
echo "\n";
}
}
echo "The H2 contents are:\n";
getRootElement["h2", $html];
echo "\n";
echo "The H3 contents are:\n";
getRootElement["h3", $html];
echo "\n";
echo "The Paragraph contents include\n";
getRootElement["p", $html];
echo "\n";
6 để tìm kiếm một phần tử hoặc thẻ HTML cụ thểĐể tìm một phần tử có
loadHTML[$html];
$dom->preserveWhiteSpace = false;
$content = $dom->getElementsByTagName[$element];
foreach [$content as $each] {
echo $each->nodeValue;
echo "\n";
}
}
echo "The H2 contents are:\n";
getRootElement["h2", $html];
echo "\n";
echo "The H3 contents are:\n";
getRootElement["h3", $html];
echo "\n";
echo "The Paragraph contents include\n";
getRootElement["p", $html];
echo "\n";
7 đặc biệt, chúng ta cần bộ chọn loadHTML[$html];
$dom->preserveWhiteSpace = false;
$content = $dom->getElementsByTagName[$element];
foreach [$content as $each] {
echo $each->nodeValue;
echo "\n";
}
}
echo "The H2 contents are:\n";
getRootElement["h2", $html];
echo "\n";
echo "The H3 contents are:\n";
getRootElement["h3", $html];
echo "\n";
echo "The Paragraph contents include\n";
getRootElement["p", $html];
echo "\n";
7 để áp dụng cho từng phần tử loadHTML[$html];
$dom->preserveWhiteSpace = false;
$content = $dom->getElementsByTagName[$element];
foreach [$content as $each] {
echo $each->nodeValue;
echo "\n";
}
}
echo "The H2 contents are:\n";
getRootElement["h2", $html];
echo "\n";
echo "The H3 contents are:\n";
getRootElement["h3", $html];
echo "\n";
echo "The Paragraph contents include\n";
getRootElement["p", $html];
echo "\n";
9. Ngoài ra, để tìm văn bản thực tế, chúng tôi cần sử dụng bộ chọn The H2 contents are:
Welcome to the Abode of PHP
Welcome to the Abode of JS
The H3 contents are:
Understanding PHP
Using PHP
Install PHP
Configure PHP
Understanding JS
The Paragraph contents include
PHP has been the saving grace of the internet from its inception, it
runs over 70% of the website on the internet
...
0 trên phần tử, sau đó chúng tôi lưu trữ trong mảngSử dụng cùng một tệp HTML như phần trước, hãy phân tích cú pháp bằng cách sử dụng
loadHTML[$html];
$dom->preserveWhiteSpace = false;
$content = $dom->getElementsByTagName[$element];
foreach [$content as $each] {
echo $each->nodeValue;
echo "\n";
}
}
echo "The H2 contents are:\n";
getRootElement["h2", $html];
echo "\n";
echo "The H3 contents are:\n";
getRootElement["h3", $html];
echo "\n";
echo "The Paragraph contents include\n";
getRootElement["p", $html];
echo "\n";
2loadHTML[$html];
$dom->preserveWhiteSpace = false;
$content = $dom->getElementsByTagName[$element];
foreach [$content as $each] {
echo $each->nodeValue;
echo "\n";
}
}
echo "The H2 contents are:\n";
getRootElement["h2", $html];
echo "\n";
echo "The H3 contents are:\n";
getRootElement["h3", $html];
echo "\n";
echo "The Paragraph contents include\n";
getRootElement["p", $html];
echo "\n";
2Đầu ra của đoạn mã là
loadHTML[$html];
$dom->preserveWhiteSpace = false;
$content = $dom->getElementsByTagName[$element];
foreach [$content as $each] {
echo $each->nodeValue;
echo "\n";
}
}
echo "The H2 contents are:\n";
getRootElement["h2", $html];
echo "\n";
echo "The H3 contents are:\n";
getRootElement["h3", $html];
echo "\n";
echo "The Paragraph contents include\n";
getRootElement["p", $html];
echo "\n";
3Sử dụng loadHTML[$html];
$dom->preserveWhiteSpace = false;
$content = $dom->getElementsByTagName[$element];
foreach [$content as $each] {
echo $each->nodeValue;
echo "\n";
}
}
echo "The H2 contents are:\n";
getRootElement["h2", $html];
echo "\n";
echo "The H3 contents are:\n";
getRootElement["h3", $html];
echo "\n";
echo "The Paragraph contents include\n";
getRootElement["p", $html];
echo "\n";
0 để phân tích cú pháp HTML trong PHP
loadHTML[$html];
$dom->preserveWhiteSpace = false;
$content = $dom->getElementsByTagName[$element];
foreach [$content as $each] {
echo $each->nodeValue;
echo "\n";
}
}
echo "The H2 contents are:\n";
getRootElement["h2", $html];
echo "\n";
echo "The H3 contents are:\n";
getRootElement["h3", $html];
echo "\n";
echo "The Paragraph contents include\n";
getRootElement["p", $html];
echo "\n";
Đối với thư viện PHP của bên thứ ba này, chúng tôi phải sử dụng trình quản lý phụ thuộc PHP có tên là Trình soạn thảo, cho phép chúng tôi quản lý tất cả các thư viện và phụ thuộc PHP của mình. Thư viện
loadHTML[$html];
$dom->preserveWhiteSpace = false;
$content = $dom->getElementsByTagName[$element];
foreach [$content as $each] {
echo $each->nodeValue;
echo "\n";
}
}
echo "The H2 contents are:\n";
getRootElement["h2", $html];
echo "\n";
echo "The H3 contents are:\n";
getRootElement["h3", $html];
echo "\n";
echo "The Paragraph contents include\n";
getRootElement["p", $html];
echo "\n";
0 có sẵn qua GitHub và cung cấp khả năng quản lý bộ nhớ và tốc độ cao hơn các thư viện khácNếu chưa có, bạn có thể cài đặt Composer tại đây. Tuy nhiên, lệnh sau sẽ thêm thư viện
loadHTML[$html];
$dom->preserveWhiteSpace = false;
$content = $dom->getElementsByTagName[$element];
foreach [$content as $each] {
echo $each->nodeValue;
echo "\n";
}
}
echo "The H2 contents are:\n";
getRootElement["h2", $html];
echo "\n";
echo "The H3 contents are:\n";
getRootElement["h3", $html];
echo "\n";
echo "The Paragraph contents include\n";
getRootElement["p", $html];
echo "\n";
0 vào dự án của bạn nếu bạn có nóloadHTML[$html];
$dom->preserveWhiteSpace = false;
$content = $dom->getElementsByTagName[$element];
foreach [$content as $each] {
echo $each->nodeValue;
echo "\n";
}
}
echo "The H2 contents are:\n";
getRootElement["h2", $html];
echo "\n";
echo "The H3 contents are:\n";
getRootElement["h3", $html];
echo "\n";
echo "The Paragraph contents include\n";
getRootElement["p", $html];
echo "\n";
7Sau đó, bạn có thể sử dụng mã bên dưới, có cấu trúc tương tự như
loadHTML[$html];
$dom->preserveWhiteSpace = false;
$content = $dom->getElementsByTagName[$element];
foreach [$content as $each] {
echo $each->nodeValue;
echo "\n";
}
}
echo "The H2 contents are:\n";
getRootElement["h2", $html];
echo "\n";
echo "The H3 contents are:\n";
getRootElement["h3", $html];
echo "\n";
echo "The Paragraph contents include\n";
getRootElement["p", $html];
echo "\n";
2 với phương thức loadHTML[$html];
$dom->preserveWhiteSpace = false;
$content = $dom->getElementsByTagName[$element];
foreach [$content as $each] {
echo $each->nodeValue;
echo "\n";
}
}
echo "The H2 contents are:\n";
getRootElement["h2", $html];
echo "\n";
echo "The H3 contents are:\n";
getRootElement["h3", $html];
echo "\n";
echo "The Paragraph contents include\n";
getRootElement["p", $html];
echo "\n";
6. Có một The H2 contents are:
Welcome to the Abode of PHP
Welcome to the Abode of JS
The H3 contents are:
Understanding PHP
Using PHP
Install PHP
Configure PHP
Understanding JS
The Paragraph contents include
PHP has been the saving grace of the internet from its inception, it
runs over 70% of the website on the internet
...
7, chuyển ngữ cảnh phần tử HTML thành các chuỗi mà chúng ta có thể sử dụng trong mã của mìnhHàm
The H2 contents are:
Welcome to the Abode of PHP
Welcome to the Abode of JS
The H3 contents are:
Understanding PHP
Using PHP
Install PHP
Configure PHP
Understanding JS
The Paragraph contents include
PHP has been the saving grace of the internet from its inception, it
runs over 70% of the website on the internet
...
8 cho phép bạn kiểm tra xem bạn có một phần tử hoặc một lớp trong chuỗi HTML của mình hay không và trả về một giá trị Boolean