When using getElementsByTagName on a title element, it returns a DOMNodeList Object

Our unique script utilizes dom to extract all the links (a tags) from a document and then iterates through child nodes to collect information. The process starts like this:

@$dom->loadHTML($str);
$documentLinks = $dom->getElementsByTagName("a");

Here is a snippet of the loop in action:

$this->count]['href']     = strip_tags($documentLink->getAttribute('href'));

Now, my next task is to fetch the title tag from each page that we are looping through. My initial approach was:

$documentTitle = $dom->getElementsByTagName("title");
$documentLinks = $dom->getElementsByTagName("a");

However, when I tried including this in the loop/array to capture the document titles, it returned "[title] => DOMNodeList Object()". How can I incorporate fetching the title tag within the loop that works on a tags/child nodes?

$this->count]['title']  = $documentTitle;

Answer №1

When you use the getElementsByTagName method, it will give you a DOMNodeList object. To retrieve the text content of the first item in this list (which should only be one page title), you can use the following code snippet:

Here's an example:

$documentTitle = $dom->getElementsByTagName('title')->item(0)->textContent;

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Error 500 in WordPress Child Theme due to AJAX Internal Issue

I have encountered an issue with my Ajax code in the Js/Jq block (/buscador/menuLateral/menu-libros.php): $.ajax({ url: '<?= get_stylesheet_directory_uri(); ?>' + '/buscador/buscador-functions.php', type: 'POST' ...

NicEdit is having difficulty saving HTML code

I recently started using NicEdit because of its lightweight nature and simple installation process. However, I encountered an issue where although NicEdit is functioning properly in changing content within the textarea, the saved posts do not contain any ...

When searching for Arabic letters in PHP MySQL, the results may not always be displayed due to their different shapes

Having an issue with searching for Arabic letters as they have different shapes, like أ إ ا ي ى ه ة Each of these letters have a different unicode value. When entering a letter such as (ا), the search query should retrieve all words containing a ...

Performing an AJAX request to the database when a change occurs, prior to submitting the

In my user setup/create form, I am including a field for the users' license/certification number which needs to be validated and returned to a specific DIV Onchange before the form is submitted. I have heard that using AJAX POST is the way to achieve ...

Uploading images and videos to separate directories using Codeigniter

Successfully uploaded the image but facing issues with uploading the video and encrypting the file name. Seeking assistance to resolve this problem. Below is the Controller: $image = $_FILES['imagearchiev']['tmp_name']; $video = $_FILE ...

experiencing difficulties in retrieving the outcome from a sweetalert2 popup

function confirmation() { swal.fire({ title: "Are you absolutely certain?", text: "You are about to permanently delete important files", type: "warning", showCancelButton: true, show ...

Exploring the Laravel rule set before today: a guide to mastering it

Encountering an issue with this rule showing a syntax error syntax error, unexpected '.', expecting ')' public static $rules = array( 'first_name' => 'required|alpha-dash', 'last_name' => &ap ...

The MySQL Query Maker

To fetch data from a database for only one row and then generate separate pieces of information, use the following code: <?php session_start(); ob_start(); error_reporting(E_ERROR | E_WARNING | E_PARSE); ini_set("display_errors", 1); set_time_limit(1 ...

Setting up a Cron Job in cPanel to Automate Task Scheduling in Laravel

Just like the title suggests, I followed the documentation to set up a cron job but it's not working. There are no updates in the laravel.log file either, which could have provided some insight. Here is how I configured my cron job: https://i.stack.i ...

Issues with Nginx serving .min.css and .min.js files

I am struggling with my Ubuntu VPS that has nginx installed on it. The issue I'm facing is related to .min.css and .min.js files, as nginx seems to be rendering empty HTML files instead of serving them correctly. Can anyone help me identify and solve ...

What is the best method for securely storing passwords in a database?

<?php if(isset($_POST['submit'])) { include ("connect/connect.php"); static function generatePassword($length = 8) { $chars = "1234567890abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; $i = 0; $password = ""; while ( ...

PHP not receiving POST information from $.ajax call

My JavaScript triggers a POST method when the datepicker loses focus, calling the script rent-fetch-pick-up-point.php. However, the PHP script gets stuck at the if-statement because it's not receiving the POST data. The datepicker is associated with a ...

Convert object to JSON format using AJAX request to a PHP file

Despite receiving a 200 green response, my data is still not getting written to the json file and it remains blank. The JavaScript: $(function() { $('form#saveTemp').submit(function() { let savdAta = JSON.stringify($('form#save ...

What steps can I take to gradually reduce the density of an array?

Having a fully-populated array of values, I am looking to remove elements from the array in an arbitrary manner, with more removals happening towards the far end. For instance, consider the following input (where a . signifies a populated index) ........ ...

if else statement fails to work in ajax-based login form

I tried to create the code below by referencing processes.php, submit form, and ajax from various sources on the internet. However, my code doesn't seem to be working properly. proccess.php <?php session_start(); ini_set('display_errors&apos ...

Combine and update JSON files by matching ID's in PHP

I am facing an issue with merging JSON files in PHP. I want to overwrite data when the id's match, but it's not working as expected. Here is my PHP code: foreach($instagram_array['data'] as $key => $image){ $id = $image['id ...

Guide on using .htaccess to redirect all extensions to files with .php after the .php extension

Google Webmaster Tools has flagged several crawl errors related to unconventional URL extensions being added to my existing pages. For example, this page appears normal: However, unexpectedly, there are these strange URLs generated from the original link ...

Querying MySQL with a large join operation combining multiple values to assist in retrieving the necessary

My query fetches most of the necessary information: SELECT cm.*, companies.company_name, companies.permalink,last_met.*,(cm.total_unique_visitors - last_met.last_UV)/last_met.last_UV * 100 as percent_change FROM calculated_metrics as cm LEFT JOIN comp ...

Ways to provide input parameters to a method upon clicking an element without using the HTML onclick attribute

Is there a way to pass data between pages without redirecting to the next.php page every time a button is clicked? One suggestion is to remove the onclick attribute from the button: <button class="submit-btn"></button> and use this jQuery fu ...

Tips for regularly retrieving information from a psql table

I have a scenario where I am retrieving data from a psql table and converting it into a JSON array to be used for displaying a time series chart using JavaScript. The data that is passed needs to be in the form of an array. Since the data in the table get ...