I already grab a page from webpage and save it in my variable $result. Now I want to explode it because I just want to get 1 row from all page which contain the information that I need. Here is an example of the HTML code that I grab.
<pre>
.....
<tr class="altRowItem">
<td class="center"></td>
<td class="center"></td>
<td class="center"></td>
<td class="fareCol1"></td>
<td class="fareCol2">
<p>
<input id="ControlGroupScheduleSelectView_AvailabilityInputScheduleSelectView_RadioButtonMkt1Fare4" type="radio" value="0~A~~A~RGFR~~1~X|QG~9743~ ~~CGK~04/18/2014 13:10~DPS~04/18/2014 15:55~" name="ControlGroupScheduleSelectView$AvailabilityInputScheduleSelectView$market1"></input>
Rp.1,137,000
</p>
</td>
</tr>
.....
</pre>
I just need string from <p> until </p>. Here is the code that I used.
$arrayResult[] = explode("<td class='fareCol2'>", $result);
echo $arrayResult[1];
There's nothing in $arrayResult[1]. Can anyone help me?
No need for [ ], also match the quotation formatting
$arrayResult = explode( '<td class="fareCol2">', $result);
Related
At the beginning I would like to say that this is my first post and I apologize in advance for any possible mistakes. And these are my beginnings in programming.
I have a problem with appropriate action download.php in the following code snippet:
<?php
$cv= $row[25];
$output .= '
<tr>
<td> <a href='download.php?dow=$cv'>Download</a> </td>
</tr>
';
?>
I wish it worked like the following code (in which it operates correctly):
<tr>
<td><?php echo "<a href='download.php?dow=$cv'>Download</a><br>"; ?></td>
</tr>
You need to concatenate the string. To concatenate you use a dot . This will put the variable value inside your string.
<?php
$output .= "
<tr>
<td><a href='download.php?dow=".$cv."'>Download</a> </td>
</tr>
";
you closed string before it's even finished!
the $output is a variable and you tried to store the HTML as an string inside it, but you've closed the ' sign before the download.php?dow=$cv and then opened it again. the correct way to write it is:
<?php
$cv= $row[25];
$output .= "
<tr>
<td> Download </td>
</tr>
";
?>
the \ before " called escaping and is needed because we dont want to end our string! just want to add that quote sign as a character like the others.
I am able to get the first word of each row of the array ($arrayQ). However, I cannot get the rest of the words. That means it jumps to the net row whenever it encouters a space. How can I fix this. Here is the code:
<?php
$i=0;
foreach ($arrayQ as &$value) {
$n =$value; ?>
<tr> <td align=left > <td align=left><pre>
<?php
echo "<input id='choice' name='arrayQ[]' type='checkbox' value=$n>";
echo $n?>
</pre></td> </pre> </td> </tr>
<?php
here is a corrected version of your code:
<?php
foreach ($arrayQ as $value) {
?>
<tr><td align="left">
<?php
echo '<input id="choice" name="arrayQ[]" type="checkbox" value="'.$value.'">';
echo $value;
?>
</td><td> </td></tr>
<?php
}
?>
I dont know why you used <pre> so i removed it.
I dont know why you closed </pre> two times so i removed it.
I dont know why you used $value as a reference so i removed it.
I dont know why you opened a <td> two times so i removed it.
I dont know why you used $n instead of $value so i removed it.
If this code continues the first word only printing you should do a
print_r($arrayQ);
to see if your array is like you expected.
I am using the PHP Simple HTML DOM Parser (http://simplehtmldom.sourceforge.net/) to read through a website and output particular information.
I'm trying to output the contents of specific ,tr, tags in every table, and the contents of specific ,p, tags, rather than all tables and all paragraphs.
Therefore, Ideally I would like to set up some PHP code that involves numeric parameters which refer target specific "nth" ,td, or ,p, tags.
As a PHP novice, I greatly appreciate the expertise that is found on StackOverflow.
Thank you for your time and assistance in figuring out my questions.
The first question set is here, above the code. The second question set can be found at the bottom of this post, with the PHP code.
1st question set:
A. How does one output the 2nd and 3rd of every table?
AND
B. How does one output the 4th paragraph after every table and exclude the ,a, tag it contains?
IN
The following HTML code
USING
The PHP Simple HTML DOM Parser as shown in the following PHP code
UNLESS
You have a different suggestion that you believe is better
Below is sample HTML code followed by PHP code and another relevant question set.
This is the main HTML I am interested in.
<a name=“arbitrary_a_tag_Begin_Item_01”></a>
<h2>Item No. 1 </h2>
<table>
<tbody>
<tr>
<td>Item Description:</td>
<td>Big blue ball</td>
</tr>
<tr>
<td>Property Location:</td>
<td>Storage Closet</td>
</tr>
<tr>
<td>Owner:</td>
<td>Gym</td>
</tr>
<tr>
<td>Cost</td>
<td>20.00</td>
</tr>
<tr>
<td>Vendor:</td>
<td>Jim’s Gym Toys</td>
</tr>
</tbody>
</table>
<p>
Approximate minimum acceptable grage sale price: $10
<br>
6 month redemption period
</p>
<p>
<img src="../dec/Item01.jpg">
</p>
<p>
<a target="new" href="http://pictures/Item01.jpg”>Picture of Item 01</a>
</p>
<p>
Current status: In Stock
<a name=“arbitrary_a_tag_Begin_Item_02></a>
</p>
<h2>Item No. 2 </h2>
<table>
<tbody>
<tr>
<td>Item Description:</td>
<td>Green tennis racket</td>
</tr>
<tr>
<td>Property Location:</td>
<td>Gear Lockers</td>
</tr>
<tr>
<td>Owner:</td>
<td>Tennis Team</td>
</tr>
<tr>
<td>Cost</td>
<td>50.00</td>
</tr>
<tr>
<td>Vendor:</td>
<td>Jim’s Gym Toys</td>
</tr>
</tbody>
</table>
<p>
Approximate minimum acceptable grage sale price: $25
<br>
6 month redemption period
</p>
<p>
<img src="../dec/Item02.jpg">
</p>
<p>
<a target="new" href="http://pictures/Item02.jpg”>Picture of Item 02</a>
</p>
<p>
Current status: In Stock
<a name=“arbitrary_a_tag_Begin_Item_03></a>
</p>
<h2>Item No. 3 </h2>
<table>
<tbody>
<tr>
<td>Item Description:</td>
<td>Red Soccer Ball</td>
</tr>
Etc. etc. etc.
The PHP code USING "PHP Simple HTML DOM Parser":
<?php
// Include the library
include('simple_html_dom.php');
$url = 'http://www.URL.com';
// Create DOM from URL or file
$html = file_get_html($url);
foreach($html->find('table') as $table)
{
echo '<table><tbody>';
foreach($table->find('tr') as $tr)
{
echo '<tr>';
foreach($tr->find('td') as $td)
{
echo '<td>';
echo $td->innertext;
echo '</td>';
}
echo '</tr>';
}
echo '</tbody></table><br />';
}
Some things I have come across and unsuccessfully attempted to implement to access specific tags:
The First Concept
$e = $html->find('table', 0)->find('tr', 1)->find('td');
foreach($e as $d){
echo $d;
}
Second concept:
$file = file_get_contents($url);
preg_match_all('#<p>([^<]*)</p>#Usi', $file, $matches);
foreach ($matches as $match)
{
echo $match;
}
Second Question Set:
Regarding this first concept above,
How do I set up a while loop to iterate through, lets say 12 tables?
For example, this: $e = $html->find('table', 0)
reads only the first table.
Yet, I am not sure how to replace the 0 with a variable, such as $i, which can be autoincremented.
$i = 1;
while($i<=12){
What goes here??
}
$i++
Regarding the second concept,
How can I use this (or the first concept) to:
Return an array of all p tags after each table
Read through the string contents (the "contents") within each p tag, and check it against string (the "key")
Only return the string "contents" when the key string is found within the contents
Before outputting the returned "contents" featuring the matched string, exclude/remove a 2nd matched string from the information to be output (for example, in the 1st Question Set, I want to grab everything within a specific ,p, tag, but exclude everything within the ,a, tag).
Thanks very much for your time and assistance!
EDIT: I solved my problem in total differnt way, thanks to those who respond anyways! helped a lot!
I need to send a table on HTML through a get variable.
The code is:
<html>
<head>
<meta charset="utf-8" />
<title>Itinerario</title>
<link rel="stylesheet" type="text/css" href="itine.css"/>
</head>
<body bgcolor="#FFC334" link="black" vlink="black" alink="white">
<?php
$html='
<font face="arial">
<center>
<table bgcolor="#F3F3F3" style="border:2px solid black;" cellpadding="10" cellspacing="10">
<tr>
<td></td>
<td>CELL 1</td>
<td>CELL 2</td>
</tr>
</table>
</center>
</font>
';
echo $html;
echo 'LINK ME';
?>
</body>
</html>
The other part its just a $_get:
<?php
$variable = $_GET['itine'];
?>
If I replace the $html in the URL for text it works fine but adding table structure sends nothing (no error), only blank.
Any ideas?
You wouldn't be able to send such complex HTML via GET, especially unescaped.
If you must pass HTML from page to page, you could base64_encode() the HTML, and probably gzcompress() it too. This would help prevent hitting the character limit for GET requests, but wouldn't eliminate the problem for very large tables.
I did once have to do something similar, which required passing a lot of data into GET variables. I had to create a couple of functions to make them GET friendly though:
function shrink($str) {
$str=strtr(base64_encode(addslashes(gzcompress($str,9))), '+/=', '-_,');
return $str;
}
function unshrink($str) {
$str = gzuncompress(stripslashes(base64_decode(strtr($str, '-_,', '+/='))));
return $str;
}
The shrink() function there gzips the string at compression level 9, and removes any characters from the encoded output that could cause issues for GET requests.
unshrink() does the opposite.
If you were to include those functions, you could do something like this in your example:
$html='
<font face="arial">
<center>
<table bgcolor="#F3F3F3" style="border:2px solid black;" cellpadding="10" cellspacing="10">
<tr>
<td></td>
<td>CELL 1</td>
<td>CELL 2</td>
</tr>
</table>
</center>
</font>
';
echo $html;
echo 'LINK ME';
Then to de-compress at the other end:
echo unshrink($_GET['itine']);
This was my first actual post here, so I hope it offers some help!
urlencode(), and no need to decode them, because when they are assigned to $_GET or $_POST they should already have been decoded.
The first problem is that you have a double quote in the HTML you want to send, this closes off the double quote of your a href.
Secondly, you might bump in the limitation of GET requests, theoretically they can only be 256 characters long.
Lastly, I would propose to you to urlencode/decode the data you try to send trough the url:
Encode:
echo 'LINK ME';
Decode:
<?php
$variable = urldecode($_GET['itine']);
?>
Well since you have some limitations about string length on $_GET, it's preferable not to do this. If you explain better what you need, maybe we can provide you another solution.
How can I get the "85 mph" from this html code with PHP + Regex ?
I couldn't come up with right regex
This is the code
http://pastebin.com/ffRH9K9Q
<td align="left">Los Angeles</td>
</tr>
<tr>
<td align="left">Wind Speed:</td>
<td align="left">85 mph</td>
</tr>
<tr>
<td align="left">Snow Load:</td>
<td align="left">0 psf</td>
(simplified example)
You've heard already about not using regex for the job, so I won't talk about that.
Let's try something here. Perhaps not the ideal solution, but could work for you.
<?php
$data = 'your table';
preg_match ('|<td align="left">(.*)mph</td>|Usi', $data, $result);
print_r($result); // Your result shoud be in here
You could need some trimming or taking whitespaces into account in the regex.
The first comment that links to the post about NOT PARSING HTML WITH REGEX is important. That said, try something like DOMDocument::loadHTML instead. That should get you started traversing the DOM with PHP.
To expand on DorkRawk's suggestion (in the hope of providing a relatively succinct answer that isn't overwhelming for a beginner), try this:
<?php
$yourhtml = '<td align="left">Los Angeles</td>
</tr>
<tr>
<td align="left">Wind Speed:</td>
<td align="left">85 mph</td>
</tr>
<tr>
<td align="left">Snow Load:</td>
<td align="left">0 psf</td>';
$dom = new DOMDocument();
$dom->loadHTML($yourhtml);
$xpath = new DOMXPath($dom);
$matches = $xpath->query('//td[.="Wind Speed:"]/following-sibling::td');
foreach($matches as $match) {
echo $match->nodeValue."\n\n";
}