Hello,

I have a simple HTML document that has a several DIVs...

<div id="17">Some content</div>
	<div id="2">Other content</div>
	<div id="23">And so on.</div>

As you can see the IDs are not sequentially numbered because they're used by a JavaScript function that reads them and takes an appropriate action. That part works perfectly.

Now, I'd like to have a PHP function read the HTML file and list the IDs. What's the simplest code for doing that? For instance, something like...

$fileName = "filename.htm";
	$file = fopen($fileName, "r") or exit("Unable to open file!");
	while(fgets($file)) {
		something, something;
	}
	fclose($file);

Result...
17
2
23

I tried using fgets and various PHP string functions but I wasn't able to consistently get the ID property.

Thanks.

Recommended Answers

All 10 Replies

Hmmmm, is this changing often. You mentioned using JavaScript to change them. Will JavaScript be changing them often?

If so then you might aswell get JavaScript to order them. Theres little point in PHP organizing them for JavaScript to come and cock them up.

Hmmmm, is this changing often. You mentioned using JavaScript to change them. Will JavaScript be changing them often?

If so then you might aswell get JavaScript to order them. Theres little point in PHP organizing them for JavaScript to come and cock them up.

Hi Josh. Thank you for your reply.

It's actually being used for two different reasons. JavaScript reads the page and, based on the div id's, displays the div content in a particular way. The IDs are associated with rows in a database. The PHP function will be reading the same file in order to fetch other info from the database.

Sorry, so are the DIV's fetched from the database? Presuming it is MySql then you can add ORDER BY column ASC/DESC Column being the column name to identify the divs and DESC meaning descending and ASC meansin ascending.

I've never used it but this looks like what you may be looking for.
PHP DOM

The DIVs and the HTML file are created manually and will be modified on occasion. The issue isn't sorting them. What I need to do is have a PHP function read the IDs so that it can fetch corresponding information from the database. JavaScript reads the same IDs for a different function. I want to use this technique so that I'm only modifying the one HTML file rather than modifying the PHP script or the database. It's easy to do, but it makes more sense to have the source information in one location.

Thank you. I actually tried that. It definitely works on the PHP side but then I get into a hassle with Javascript reading the same elements. I don't mind parsing XML on the server side but, due to different browser implementations, I'm not comfortable relying on that technique in the browsers (which is what I would need to do in order to have Javascript parse it).

Ignore this post

I was able to get this to work. It may not be perfect but from the top of my head, it is a start. If this is not what you wanted, then I just don't understand the question.

<?php
$url = "http://localhost/test.html";
$html = file_get_contents($url);

$parts = explode('<div id="', $html);
$divids = array();

array_shift($parts);
foreach($parts as $data)
{
	$divids[substr($data, 0, strpos($data, '"'))] = findStringBetween('<div id="' . substr($data, 0, strpos($data, '"')) . '">', '</div>', '<div id="' . $data);
}
print_r($divids);

function findStringBetween($begin, $end, $string)
{
	$pos1 = strpos($string, $begin);
	$pos2 = strpos($string, $end);
	
	if($pos1 === false || $pos2 === false)
	{
		return false;
	}
	
	return substr($string, $pos1 + strlen($begin), $pos2 - ($pos1 + strlen($begin)));
}
?>

Baldy, this is exactly what I was looking for. Thank you very much for taking the time.

Baldy, this is exactly what I was looking for. Thank you very much for taking the time.

No problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.