Get first HTML-element in QueryPath using find - php

I am trying to retreive one specific element from HTML-code using QueryPath. It occurs twice, I only want the first one though.
Searching for the object DOES work, but it returns me two elements.
I was trying to add a pseudo-class-selector to my search, but that didn't work.
This is the HTML-element that occurs twice in the code:
<span class="aui-suffix"> of 5 </span>
And this is how I am searching for it:
$arrURL = "URL..."
$html = htmlqp( $arrURL );
$pageAsString = $html->find('span.aui-suffix');
echo $pageAsString->text();
The output is "of 5 of 5 ", which is both elements printed right after each other.
How can I modify my search to get me only "of 5 "?

try
$pageAsString = $html->find('span.aui-suffix:eq(0)');

Related

Get specific URL with PHP Simple HTML DOM Parser

So for my code I basically need to get a specific URL from a webpage, I use Simple HTML DOM Parser, so far I managed to get all the links from the webpage with the class .linkify, which results in having 18 different links, and I will need to have only the second one in a variable.
Here's my code:
$html = file_get_html("http://saucenao.com/search.php?db=999&url=http://simg4.gelbooru.com//images/4f/3d/$file");
foreach($html->find('a.linkify') as $element)
echo $element->href . '<br>';
How do I make it so it only generates the second link?
Thanks!
You can select which one you want by modifying
$html->find('a.linkify')
to
$html->find('a.linkify', 1)
To fetch the 2nd one, we use ,1. It's index based so we start at 0 (0 = First one, 1 = Second one)
To get the href in a single line, do the following:
$html->find('a.linkify', 1)->href

How to count number of lines from HTML out in PHP

How can I count the number of tags in HTML with PHP? I have a website which shows all of the bans on my game server, I want to count the number of bans that have been done, I see the only way to do this being counting the number of tags, since the HTML output is all on one line. At the moment I have:
$content = file_get_contents('**WEBSITE OF BAN LIST HERE**')
The HTML output looks like this but much much longer:
hillel123 banned on 13/March/2014 with reason : None<br>xmrbrhoom banned on 13/March/2014 with reason : None by [name of banner]<br>InfinityJoris banned on 13/March/2014 with reason : None by [Name of banner]<br>
Thanks
You could figure out how many bans there are by counting the occurrences of <br>? As long as the html is in that form..
echo substr_count($html, '<br>'); // How many new lines there are?
You can count tags using this code:
$dom = new DOMDocument;
$dom->loadHTML($HTML);
$allElements = $dom->getElementsByTagName('*');
echo $allElements->length;
Instead of (*) you can put any tags you want and you'll get number of those tags.
The easyest way i can think of is that you make an array and then count it.
Since you have all that in one string you can easely do that :)
<?php
$content = file_get_contents('**WEBSITE OF BAN LIST HERE**');
$banns = explode('<br>', $content);
echo count($banns);
?>
I think you want to use file instead of file_get_contents. Then use the count() function to get the resulting array's length.

Using PHP SimpleXML to parse an oBix XML feed?

I am rather new to development, but a long time sys-admin. I am trying to retrieve a specific value from the following XML feed. (Its oBix).
http://80.68.58.47:900/obix/config/Drivers/NiagaraNetwork/OSS_Tridium_Demo/points/kW_Sys/
I am trying to return the "Out" value within the 'Real' element (If element is the right name for it). The value is a number next to val=.
I have spent a fair few hours trying to work out how to isolate the 'val' attribute and return the number but I can't seem to do it. I can isolate the 'real' element but can't go any further in to get the actual number value.
Could someone show me how this is done using PHP SimpleXML so I can try to get my head round it?
This is my code so far:
<?php
$url = "http://80.68.58.47:900/obix/config/Drivers/NiagaraNetwork/OSS_Tridium_Demo/points/kW_Sys/";
$xml = simplexml_load_file($url);
$elementselection = $xml->real[0];
$outputvalue = $elementselection;
print_r ($outputvalue);
?>
Many Thanks!
Tom
If you just want the value from the first 'real' element you can use the following:
$url = "http://80.68.58.47:900/obix/config/Drivers/NiagaraNetwork/OSS_Tridium_Demo/points/kW_Sys/";
$xml = simplexml_load_file($url);
$value = (string) $xml->real[0]['val'];
var_dump($value);

How to format text within a p tag using PHP DomDocument

I've seen many question which are almost what I am looking to do and have led me to almost be able to get it but not quite.
I want to format text as follows within a <p> tag which is within a div.
Page 1 of 2
So in ordinary HTML I used the b tag within the paragraph but can't seem to figure out how to do that with DomDocument. When I try to create an element like so
$pTag = $dom->createElement("p", "Page <b>1</b> of 2");
It outputs just that without recognising the as HTML. So I thought about it and came up with
$pTag->nodeValue .=
as a way to append a new element but that did no good. It didn't give me any errors but it didn't append the <b> tag either. This seems like something that should be simple but doesn't seem to be.
When I tried echo it outputted the text to the top of the screen, not where I wanted it.
I'd appreciate any advice.
Something like the following should work:
$bTag = $dom->createElement("b", "1");
$pTag = $dom->createElement("p");
$pTag->appendChild($dom->createTextNode("Page "));
$pTag->appendChild($bTag);
$pTag->appendChild($dom->createTextNode(" of 2"));

Str_replace not removing tags in message string

I echoing the contents of a table using foreach, the problem I am getting is the date is always at the top of the page, so when looking at my one line preview I just get the date and nothing more. If I remove the date it is fine and you can see the first line of the template. So I am trying to remove the date tag from the preview using for each.
I am using the following code, but it doesnt seem to be doing a lot, as far as I can see I have things right ?.
echo '<div class="messagerow">';
// this pulls the message from the foreach above it
$emlmsg = $row->emailformmessage;
// I am trying to remove the date by replacing it with the blank contents of this string value.
$dateremove = '';
// This is the str_replace that is supposed to remove the {date} tag along with the paragraph tags it is wrapped in.
$emlmsgfiltered = str_replace(array('<p>{date}</p>'), array($dateremove), $emlmsg);
// I then echo the filtered message here, minus the date....but its still there ??
echo $emlmsgfiltered;
echo '</div>';
Edit >>>>>>
As requested, this is the html code
<p>{date}</p>
<p>Dear {name} thankyou for your order, if you need any more oil we will be happy to provide you with a competitive quote.</p>
<p>Kind Regards</p>
{createdby}
sorry . should have been an ansewr, not a comment:
why the array around the first value in str_replace()? are you supposed to be referencing an array element
str_replace('<p>{date}</p>',$dateremove,$emlmsg)
It seems to me that '<p>{date}</p>' isn't being found. You can try to recreate the date here by typing something $date = date('Y-m-d') then '<p>'.$date.'</p>' or "<p>$date</p>"
However, the dates may not match up correctly in all cases.
You can also try to pull the date in from somewhere. That would be ideal.

Categories