Hướng dẫn php get element by name - php lấy phần tử theo tên

Tôi đang cố gắng cụm từ nguồn HTML bằng cách sử dụng cụm từ HTML DOM, tôi cần nhận được một giá trị bên trong thẻ đầu vào, tôi đã thử điều này:

foreach ($doc->getElementsByTagName('input') as $link)
{
    $links[] = array(
        'value' => $link->getAttribute('value'), 
        'text'  => $link->nodeValue,
    );
}

Điều này hoạt động với tôi, nhưng trang web của tôi có nhiều thẻ đầu vào hơn, nhưng tôi muốn nhận được giá trị của thẻ đầu vào được chỉ định.

Giả sử nó,


Tôi đã cố gắng sử dụng getElementsByTagName, nhưng nó cho tôi một lỗi.

(Php 5, Php 7, Php 8)

DomDocument ::Searches for all elements with given local tag name

Sự mô tả

PublicDomDocument :: DOMDocument::getElementsByTagName(string $qualifiedName): DOMNodeList

Thông số

qualifiedName

Tên địa phương (không có không gian tên) của thẻ để khớp với. Giá trị đặc biệt * khớp với tất cả các thẻ.

Trả về giá trị

Một đối tượng Domnodelist mới chứa tất cả các yếu tố phù hợp.DOMNodeList object containing all the matched elements.

Ví dụ

Ví dụ #1 Ví dụ sử dụng cơ bản

$xml = <<< XML


 Patterns of Enterprise Application Architecture
 Design Patterns: Elements of Reusable Software Design
 Clean Code

XML;$dom = new DOMDocument;
$dom->loadXML($xml);
$books $dom->getElementsByTagName('book');
foreach (
$books as $book) {
    echo 
$book->nodeValuePHP_EOL;
}
?>

Ví dụ trên sẽ xuất ra:

Patterns of Enterprise Application Architecture
Design Patterns: Elements of Reusable Software Design
Clean Code

Xem thêm

  • DomDocument :: getElementsByTagNamens () - Tìm kiếm tất cả các yếu tố có tên thẻ đã cho trong không gian tên được chỉ định

James L ¶

14 năm trước

Return if there are no matches is an empty DOMNodeList. Check using length property, e.g.:

$nodes=$domDocument->getElementsByTagName('book') ;
if (
$nodes->length==0) {
  
// no results
}
?>


0

Philip n ¶

12 năm trước


1


2


3


0

Calvin tại G Mail ¶

12 năm trước


5


6


7


8

Calvin tại G Mail ¶

gurmukh24 tại gmail dot com ¶


9

Patterns of Enterprise Application Architecture
Design Patterns: Elements of Reusable Software Design
Clean Code
0

Patterns of Enterprise Application Architecture
Design Patterns: Elements of Reusable Software Design
Clean Code
1

Patterns of Enterprise Application Architecture
Design Patterns: Elements of Reusable Software Design
Clean Code
2

13 năm trước

đá quý tại rellim dot com ¶

Patterns of Enterprise Application Architecture
Design Patterns: Elements of Reusable Software Design
Clean Code
3

Patterns of Enterprise Application Architecture
Design Patterns: Elements of Reusable Software Design
Clean Code
4

Patterns of Enterprise Application Architecture
Design Patterns: Elements of Reusable Software Design
Clean Code
5

Patterns of Enterprise Application Architecture
Design Patterns: Elements of Reusable Software Design
Clean Code
6

18 năm trước

Yaakov dot moddel tại gmail dot com ¶

Patterns of Enterprise Application Architecture
Design Patterns: Elements of Reusable Software Design
Clean Code
7

Patterns of Enterprise Application Architecture
Design Patterns: Elements of Reusable Software Design
Clean Code
8


0

10 năm trước

rsvvuuren tại hotmail dot com

getElementsByTagName0

getElementsByTagName1

getElementsByTagName2

getElementsByTagName3

getElementsByTagName4

getElementsByTagName5

getElementsByTagName6

getElementsByTagName7

8 năm trước

Simon tại onepointltd dot com

getElementsByTagName8

getElementsByTagName9

$qualifiedName0

$qualifiedName1

9 năm trước

12 năm trước

$qualifiedName2

$qualifiedName3

$qualifiedName4

$qualifiedName5

Calvin tại G Mail ¶

gurmukh24 tại gmail dot com ¶

$qualifiedName6

$qualifiedName7


0

13 năm trước

12 năm trước

$qualifiedName9

qualifiedName0

qualifiedName1

qualifiedName2

qualifiedName3

Calvin tại G Mail ¶

12 năm trước

qualifiedName4

qualifiedName5

qualifiedName6

qualifiedName7

qualifiedName8

Calvin tại G Mail ¶

14 năm trước

qualifiedName9

*0

*1

*2

*3


0

Philip n ¶

12 năm trước

*5

*6

*7

*8


0

Tôi có thể sử dụng getEuityByid trong PHP không?

DOMDocument :: GetEuityById () hàm là một hàm sẵn có trong PHP được sử dụng để tìm kiếm một phần tử có ID nhất định. Tham số: Hàm này chấp nhận một tham số $ ElementID duy nhất giữ ID để tìm kiếm. Giá trị trả về: Hàm này trả về Domelement hoặc NULL nếu phần tử không được tìm thấy.. Parameters:This function accepts a single parameter $elementId which holds the id to search for. Return Value: This function returns the DOMElement or NULL if the element is not found.

Nhận phần tử là gì bởi TagName?

getElementsByTagName () phần tử.phương thức getelementsByTagName () trả về một htmlcollection trực tiếp của các phần tử với tên thẻ đã cho.Tất cả các hậu duệ của phần tử được chỉ định đều được tìm kiếm, nhưng không phải là phần tử.Danh sách được trả lại là trực tiếp, có nghĩa là nó tự động cập nhật với Dom Tree.returns a live HTMLCollection of elements with the given tag name. All descendants of the specified element are searched, but not the element itself. The returned list is live, which means it updates itself with the DOM tree automatically.

Làm thế nào tôi có thể gọi ID HTML trong PHP?

PHP không thể "gọi một điều khiển HTML".Nếu bạn muốn tự động sửa đổi mọi thứ trong trình duyệt, bạn sẽ phải thực hiện phía máy khách bằng JavaScript.Đó là phía máy khách, vì vậy bạn phải sử dụng JavaScript, không phải PHP.. If you want to dynamically modify things in the browser, you'll have to do it client-side using Javascript. It is client-side, so you have to use Javascript, not PHP.

Làm cách nào để có được một yếu tố trong HTML?

Tìm các phần tử HTML của các bộ chọn CSS Nếu bạn muốn tìm tất cả các phần tử HTML khớp với bộ chọn CSS được chỉ định (ID, tên lớp, loại, thuộc tính, giá trị của thuộc tính, v.v.), sử dụng phương thức QuerySelectorall ().Ví dụ này trả về một danh sách tất cả các yếu tố có lớp = "giới thiệu".use the querySelectorAll() method. This example returns a list of all

elements with class="intro" .