The answer:
As mario suggests, $parser is an Object. I checked this by simply using print_r:
print_r($parser);
I had to employ preg_split() before using the parser.
The original question:
I'm trying to simplify HTML to grab a temperature pattern. I would like to begin by narrowing down on the part of the HTML that I need by splitting the code, but my code is not working. Do I need to comment out the exclamation ?
I would like to split it at :
$fourday_start = preg_split('/<!--START NEXT FOUR DAYS -->/', "$parser");
and am getting:
Array ( [0] => Object )
sample code
</div>
<!--END TODAYS FORECAST -->
<!--START NEXT FOUR DAYS -->
<div class="FourDayForecastContainer">
<div class="FourDayForecastContainerInner">
Related
I have a xml file, I want read node value along with comments before the tag and move to the mysql database. I have tried with DOM, i can read all comments in a separate array and node values in separate array can't match exact comment to exact node value.tried in PHP can any one help in PHP or python
<ig:prescribed_property property_ref="0161-1#02-012537#1" is_required="true" combination_allowed="false" one_of_allowed="false">
<dt:controlled_value_type representation_ref="0161-1#04-000006#1">
<!-- NOT PROVIDED(Code:C,Table:AB22) -->
<dt:value_of_property value_ref="0161-1#07-006593#1"/>
<!-- PROVIDED(Code:B,Table:AB22) -->
<dt:value_of_property value_ref="0161-1#07-006627#1"/>
</dt:controlled_value_type>
</ig:prescribed_property>
eg:-0161-1#07-006593#1 ->NOT PROVIDED(Code:C,Table:AB22)
like this i want to read, in java we can do this with lexicalhandler and contenthandler
First of all I know this question(or similer) has been already asked for several times, but I didn't get 100% perfact answer anywhere.
I want to wrap each 3 array elements in a container div. like:
Array $arr = [0,1,2,3,4,5,6]; should be represented like below:
<div class="container">
<p>0</p><p>1</p><p>2</p>
</div>
<div class="container">
<p>3</p><p>4</p><p>5</p>
</div>
<div class="container">
<p>6</p>
</div>
Note: $arr can have any no of elements (not fixed).
I have found lot of post which gives above result, but they have issues with HTML. They are not generating 100% correct HTML(Keep left uncompleted HTML tags for last container tag). I want to achieve same result with valid HTML i.e all tags should be completed properly.
Note: I want to achieve it using simple loops and variables(wihtout using any built in array functions etc.).
This answers is pretty close, but has uncompete HTML tags.
Wrapping a div around every third item in a foreach loop PHP
Any help would be appreciated.
Thanks
you need for loop and steps 3 for example:
<?php for($i=0; $i<count($arr);$i+3): ?>
<div class="container">
<p>$arr[0+$i]</p><p>$arr[1+$i]</p><p>$arr[2+$i]</p>
</div>
<?php endfor; ?>
and don't forgot check array item by function isset($arr['key'])
For example, this is top-most part of "how-to.html"
1 <!-- Table of Content -->
2 <ol>
3 <li>Intro</li>
4 <li>Conclusion</li>
5 </ol>
6 <!-- / Table of Content -->
7
8 <article>
9 <h2>Introduction</h2>
10
The content between "!-- Table of Content --" is not fixed and may extend or shrink.
How to only put the contents between the HTML comments into an array or a string?
I tried to search on the method however I was unable to find anything like this. The things I found are about changing contents between lines. Since "how-to.html" will grow overtime, I need to only read the content from specified texts, not lines.
You can provide a modified example of the "how-to.html" to reflect your answer.
You can load the file into an array using file().
$file = file('filename.html');
Then you can use array_splice() to insert into a portion of it.
$newlines = array(
'<ol>',
'</ol>'
);
$file = array_splice($file, $line_num_to_insert_at, 0, $newlines);
You will still need a way to find the line numbers, I'll leave that up to you to do some research.
array_splice can insert into an array at a certain position and also replace existing content in the array. Look at the manual for more info on how to use it. The function I provided only inserts, it doesn't replace.
From the official manual I know that I can get all the comments with the following code:
// Find all comment (<!--...-->) blocks
$es = $html->find('comment');
But this creates an array of comment nodes. I want to get the content of the comments as string. How could I do that?
I've tried with $es->plaintext, $es->innertext and $es->outertext.
Here is an example of what I want:
HTML:
...
<div id='a'>
<!-- Some text -->
</div>
...
PHP:
...
$content = $html->find('div[id=a]', 0)->find('comment', 0)->some_attr;
echo 'Content:'.$content;
Browser:
Content: Some text
Thanks in advance !
I've found the solution!
When we load an html with SimpleHTMLDom, the comments (scripts and others things) are removed from document and saved inside an array called 'noise'.
We can get a comment/script/etc searching an string pattern in the whole list of noises and there is a function to do that.
This is the solution:
$html->search_noise($subString);
So, in my own example, the solution can be:
1.- $comment = $html->search_noise('Some');
2.- $comment = $html->search_noise('text');
3.- $comment = $html->search_noise('me te');
4.- etc etc
The search_noise function returns the first noise that match the pattern, so, we have to be a little careful with the chosen sub-string.
using file_get_contents, I open an Internet URL and get the contents of this webpage.
Inside the HTML there are many identical span class tags:
<span class="always-the-same-class">always dynamic text</span>
Now, I want to get an array containing all the "dynamic text" contained in any of this tags. It is not necessary to eliminate duplicated entries (I need them).
Is this possible? How could I do?
If I understood correctly, this has to be PHP as it is on the server, not in the browser. So I'd do something like
$html=file_get_contents(HTML_URL);
$a=preg_match_all("/\<span class\=\"always-the-same-class\"\>(.*?)\<\/span\>/",$html,$b);
echo $a;
print_r($b[1]);
$a has hit count, $b[1] the hits
Tested this against
<html>
.. blah ..
<body>
.. blah ..
<span class="always-the-same-class">always dynamic text A</span>
<span class="always-the-same-class">always dynamic text B</span>
<span class="always-the-same-class">always dynamic text C</span>
.. blah ..
</body>
</html>
and output was
3
Array
(
[0] => always dynamic text A
[1] => always dynamic text B
[2] => always dynamic text C
)
jquery:
var spanText = $('.always-the-same-class').text();
You can parse this content using the DOMDocument class that is provided in PHP. Once you load the content into the dom document you can then filter out the span tags by using
$content->getElementsByTagName('span');
Once you have done this then you can filter the results by the tags attributes and get the content.