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.
Related
I have a site from which I want to get specific content from 7 posts. Those all 7 seven posts have same HTML layout (See Below)
<div class="eventInfo">
<h3>Z's(矢沢永吉)</h3>
<h4>Z's TOUR 2015</h4>
<dl>
<dt><img src="/event/img/btn_day.png" alt="公演日時" width="92" height="20"> </dt>
<dd>
<table width="99%" border="0" cellpadding="0" cellspacing="0">
<tbody><tr>
<td width="9%" nowrap="nowrap">2015年6月</td>
<td width="74%">4日 (木) 19:00開演</td>
</tr>
</tbody></table>
</dd>
<dt><img src="/event/img/btn_price.png" alt="料金" width="92" height="20"> </dt>
<dd>S¥10,500 A¥7,500 (全席指定・消費税込)<br><span class="attention">※</span>注意事項の詳細を矢沢永吉公式サイトより必ずご確認ください</dd>
<dt><img src="/event/img/btn_ticket.png" alt="一般発売" width="92" height="20"> </dt>
<dd>
<table width="99%" border="0" cellpadding="0" cellspacing="0">
<tbody><tr>
<td width="9%" nowrap="nowrap">2015年5月</td>
<td width="74%">16日(土)</td>
</tr>
</tbody></table>
</dd>
<dt><img src="/event/img/btn_contact.png" alt="お問合わせ" width="92" height="20"> </dt>
<dd>ソーゴー大阪 06-6344-3326</dd>
<dt><img src="/event/img/btn_info.png" alt="公演詳細" width="92" height="20"> </dt>
<dd>http://www.siteurl.com </dd>
</dl>
</div>
I just want to fetch the H3 from this layout and the first table in the code. What regex method should I use to get the desired results?
Also these are 7 posts just like the code above and I have to get H3 and the first table from each of it.
I have tested it but not sure that is it a correct way or not: https://regex101.com/r/sO6tJ8/1
But as you can see that I have to add unwanted data too like H4 DT IMG :(
I don't think regex is your best choice here. If you can get away without using regex I would do so. Have a look at Goutte a PHP web-scraper.
$crawler = $client->request('GET', 'http://www.example.com/some-page');
$heading = $crawler->filter('h3')->first();
$table = $crawler->filter('table')-> first();
This will not only be better readable, it will also make it easier to fix something when the html-structure changes.
If you must choose regex you could do something like the following for the h3 (haven't tested it, but something like that):
$html = preg_replace_callback(
'/<h3>(.*?)<\/h3>/u',
function ($match) {
return $match[1];
},
$html
);
For the table it is similar only you have to use the multiline-modifier m (it wouldn't hurt to add it to the h3 as well, but from your example you don't need it).
// I'm assuming you can get the HTML into a variable somehow
// I did my testing w/ a local file with your HTML content
$data = file_get_contents('foo.html');
$h3_content = array();
$table_content = array();
// h3 content is easy to grab, but it could be on multiple lines!
// I didn't account for multiline here:
preg_match('/<h3>([^<]+)<\/h3>/', $data, $h3_content);
// regex can't find the ending table tag easily, unless the
// entire HTML on one line, so make everything one line
// you don't need a new variable here, I did it only to be explicit
// that we have munged the original HTML into something else
$data2 = str_replace("\n", '', $data);
// to separate tables, put new line after each one
$data2 = str_replace('</table>', "</table>\n", $data2);
// now regex is easy
preg_match_all('/(<table.+<\/table>)/m', $data2, $table_content);
echo $h3_content[1], "\n";
echo $table_content[0][1], "\n";
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);
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!
I am likely to get b'*ch slapped because I haven't searched the forum enough before posting, but I really think I searched all relevant posts already, many seem not specifically covering the question I have, the others fly right over my beginner's head ( as I am new to PHP & js ). That being said...
I have built a PHP code to fetch data from an XML file using the $xml=simplexml_load_file();
No worries there, however one of the needed data 'entries' or 'fields' needs to exist within an onclick and/or an onmouseup javascript function.
The code is as follows:
<?php
$xml=simplexml_load_file('prod_test_feed_sameday.xml');
$max = 8;
$i = 1;
foreach($xml->product as $feed){
if ($i <= $max){
echo "<table id='{$feed->position}' class='prod_container'>
<td class='hidden_mask' id='{$feed->position}'>
</td>
<td class='hidden_image' id='{$feed->position}'><span style='background:url({$feed->prod_image_large}) no-repeat center;'/></span><div><a onmouseup='$('.hidden_image#{$feed->position}').slideToggle('slow');' onclick='$('.hidden_mask#{$feed->position}').hide();'><b>CLOSE</b></a></div>
</td>
<tr>
<td class='prod_image' id='{$feed->position}'>
<span style='background:url({$feed->prod_image_small}) no-repeat center; background-size:contain;'/></span>
</td>
<td rowspan='1' class='info_toggle' id='{$feed->position}'><a onmouseup='$('.hidden_image#{$feed->position}').slideToggle('slow');' onclick='$('.hidden_mask#{$feed->position}').show();><img src='images/zoom_02.png' title='See a larger image of these flowers' /></a>
</td>
</tr>
<tr>
<td colspan='2' class='prod_name' id='{$feed->position}'>{$feed->prod_name}
</td>
</tr>
<tr>
<td colspan='2' class='prod_price' id='{$feed->position}'><span id='{$feed->position}'>from: £{$feed->price}</span><a href='{$feed->prod_link}' target='_blank'><span class='buy_button'> Buy it now! </span></a></td>
</tr>
</table>";
$i++;
}
}
?>
The data and the CSS all work perfectly. There is a href link towards the end of the code, which also works perfectly.
I am racking my brain trying to find the error in my syntax within the onlick function.
I have tried all manners of backslashing, using trial and error, for exampel:
onclick='$(\'.hidden_mask#{$feed->position}\').hide();' or
onclick='\'$('.hidden_mask#{$feed->position}').hide();\'' or
onclick=\''$(\'.hidden_mask#{$feed->position}\').hide();\'' or even
onclick=\''$(\\'.hidden_mask#{$feed->position}\\').hide();\'' <--Freddy Krugar 'slashing' example
At any rate I am at a loss.
Try with double quotes and escape them:
echo " ...
onclick=\"$('.hidden_mask#{$feed->position}').hide();\"
...";
Or
echo " ...
onclick='$(\".hidden_mask#{$feed->position}\").hide();'
...";
DEMO
This way you do the escaping in the PHP only, without needing the Freddy Krugar escaping for the DOM parser.
I have a large form, in the end of the form a user is presented with a summary:
You have entered:
<table>
<tr>
<td>First name</td>
<td><?php echo $firstname ?></td>
</tr>
<tr>
<td>Last name</td>
<td><?php echo $lastname ?></td>
</tr>
</table>
I first designed the summary page and now the thought came to me that it would be nice to send the user this page as a confirmation e-mail. What I have done now is this:
<?php $summarypage = "<table>
<tr>
<td>First name</td>
<td>".$firstname."</td>
</tr>
<tr>
<td>Last name</td>
<td>".$lastname."</td>
</tr>
</table>";
echo $summarypage; ?>
For loops I use $summarypage .= "blabla"; within the loops.
When sending the e-mail I can just take $summarypage and attach it to my e-mail body. Beautiful.
Is what I am doing OK? It seems very "not elegant" to me.
Isn't $summarypage being rendered totally anew when I call it again in my e-mail - meaning all variables concatenated with it (e.g. $firstname) will be called again - performance hog?
Is there some kind of "buffer" I could just write the $summarypage variable to, so I have a plain-text variable afterwards? Would $newsummarypage = string($summarypage) do the trick?
Ignore performance on that level, it won't matter. If the method you show works for you, use it.
An alternative that makes things a bit more readable (because you won't need PHP openers / closers) is HEREDOC:
<?php $summarypage = <<<EOT
<table>
<tr>
<td>First name</td>
<td>$firstname</td>
</tr>
<tr>
<td>Last name</td>
<td>$lastname</td>
</tr>
</table>
EOT;
?>
I think you are a bit confused about how strings/variables work in php. A little example might help
$s = "hello"; //stores the sequence 'h' 'e' 'l' 'l' 'o' in $s
$s = $s." world";//take the sequence stored in $s add the sequence ' world',
//and store in $s again
echo $s; // prints 'hello world'. That is what $s contains, what $s is
$summary = "<div>".$firstname."</div>"; // take '<div>', lookup what $firstname
//contains and add that, then add '</div>' and then store this
//new string thing('<div>Ishtar</div>') in $summary.
echo $summary; //$summary here knows nothing about $firstname,
//does not depend on it
All variables are evaluated when you use them. (Well, most of the time you use variables by evaluating them.)
$summarypage is a string, so it's just a bit of data assigned to a variable - a plain-text variable, as you said. Once the data is assigned to $summarypage, the work is done. You can happily write it out into pages, emails, databases and text files with no additional performance hits related to $summarypage.