php plaintext output compatibility with google sheets - php

I made a script in PHP to fit into a template I made in google sheets. I made the output text for convenience:
header("content-type: text/plain");
I didn't need html when I'm going for the =IMAGE or whatever. Now, I'm trying to make it so I can brainlessly copy and paste the output from my browser to the google sheets. "\t" worked completely fine for newlines, but "\t" is having some issues where google sheets thinks every single space in the tab is an actual space, so it gives an #ERROR! when i try to do multiple tabs at once.
the error: https://puu.sh/IOp2B/34b7f5ce50.png
here's my code:
$tab = "\t";
echo '=IMAGE("https://www.smogon.com/forums//media/minisprites/' . strtolower($final) . '.png")' . htmlspecialchars($tab) . $key . htmlspecialchars($tab) . substr(strval(($value/count($allteams) * 100)), 0, 5) . '%' . $tab . $value . "\n";
I tried "\t" itself, no htmlspecialcharacters, but copying directly from the webpage is consistently making it multiple spaces instead of a tab. I also tried with different browsers.
Here are my questions:
Is there a function to make the tabs copy as tabs instead of what it does now?
Is there a google sheets thing I can do to make another character (not tab) split up the columns? If so, I will replace the tab in my code with a semicolon or something, I tried data -> split columns but it doesn't work for a blank template, only when data is pasted. I want to make the data paste perfectly with no fiddling from the user.
I don't think is a browser thing, I tried like 3 different ones and the issue persists. Also, if I put the output into pastebin/text editor, it works well after, but I don't want the user to HAVE to do this.
I am going to look into importxml, but I have no info on it yet... just thought I'd ask
tldr my plaintext output html code has been great but its not separating columns well for my google sheet template.

Instead of using tab or \t, write your formula as follows
echo '={image("https://i.stack.imgur.com/5iCFq.png"),"other info","next info"}'
use , (or ; according to your locale) to separate the different cells and curly brackets { }
include specific information as usual in php with . and $

Related

I'm having an issue with PHP and Append File

I'm trying to use file_put_contents to manage bans in a .txt
However, I'm having trouble adding text or a new line amidst the text I'm adding.
I'm using $_GET to grab the reason and information of the banned person, i.e "loser,127.0.0.1" (simple example) and then add them to the txt. Thing is I can't figure out how to add a new line. When I try adding text
<?php
file_put_contents("banned.txt", $_GET["r"], + "for example here", FILE_APPEND);
The code fails to run, I'm not sure whether or not to actually have a comma either.
This is the code I'm trying to use as of now, and it does add a line, but it doesn't go to the next line.
<?php
file_put_contents("banned.txt", $_GET["r"], FILE_APPEND);
What I'm trying to achieve is that it adds a new line, so if I said "loser,127.0.0.1" it adds that text to the txt, and goes to the next line for the next ban.
Try this
get your ban data and explicitly add the new line:
$banData = $_GET["r"] . PHP_EOL;
If you want to write csv data in the file (in a very simple way) you can do so like this:
$banData = $_GET["ban_data"] . ";" . $_GET["ban_reason"] . PHP_EOL;
then simply write to the file
file_put_contents("banned.txt", $banData, FILE_APPEND);
instead of "banned.txt" save to "banned.csv" and you're set
First code is invalid as you should not have , between your GET reference and concatenated string. Once that is fixed, just add \n (\r\n on Windows) at the end of your string that you append and you should have new lines (or to stay platform agnostic, use PHP_EOL instead).

Want to use htmlspecialchars , but Hypelinks must be clicakble in view - PHP , HTML

Hi I am implementing a comment system . When showing my comments and articles in my view again , i want to use htmlspecialchars but i want my hypelinks can be clicked .
Example Comment :
My favourite web site is < www.facebook.com >.
Then i the backend i change this : to
My favourite web site is < www.facebook.com >
and save in the database .
now i am showing this comment in view , if i used echo htmlspecialchars($message) the message will be
My favourite web site is < www.facebook.com >
But i want my link to be a hyperlink , but other part should be using htmlspecailchars
I can check for the hypelink in the string and do some complex logic to do add htmlspecailchars only to other parts . I have two questions .
Is it good practice to add a hyperlink to the database or it should be created when viewing data ?
2.What is a better way to use htmlspecialchars for only strings other than hyperlinks .
like exclude only the <a> tags
in htmlentities function i saw optional character-set parameter A string that specifies which character-set to use . is there anything like exclude character set .
Thanks in advance
Your database should store raw data (sanitized for sure but not transformed). Thus the transformation link → link should happen just before view time, not before DB storage.
As for the why you should store raw data: Nothing tells you that one day you won't use your data in other things than HTML, thus the formatting would have to be cleaned up for this other use.
To sum up:
You should store raw (sanitized for SQL)
Retrieve raw
Sanitize HTML
Transform links
Display

strings get randomly truncated in PHP

I have an issue with a PHP script I'm developing. Sometimes (very rarely, but it occurs) strings don't get concatenated correctly, as fragments of the string get lost during the process, resulting in randomly truncated strings.
This is an example, it's part of the code that gathers the order info and sends it via email to the client (that's why I'm forced to use html tables).
$rowList[$rowCode] .= '<tr><td class="lens-price-serv"><small>' . $lang['services'] . ':</small> <strong>' . money_format( '%i', $srvPriceTotal ) . '</strong></td></tr>';
The code works just fine. This morning, though, I got an email with the following html (corresponding to the PHP code I posted):
<tr><td class="lens-price-serv"><s>Services: <strong>€ 10,00</strong></s></td></tr>
I couldn't get the exact html code, this one comes from Chrome's Developer Tools. What I think happened is that the 'small' tag got truncated during string concatenation, becoming an open 's' tag. This tag, without the corresponding closure '/s' tag, got automatically closed by the browser just before closing the 'td'.
This issue is hard to replicate. I tried making a new order using the same parameters, but it worked just fine.
This is the second time this issue presented itself; the first time it was on another part of the code, but the string got truncated as well, resulting in wrong output.
Could this issue be caused by the server? This script is hosted by a shared hosting running Apache 2.2.24 and PHP 5.3.21.
Try to use
$rowList[$rowCode] .= "<tr><td class='lens-price-serv'><small>$lang['services']:</small> <strong>" . money_format( '%i', $srvPriceTotal ) . "</strong></td></tr>";
You can also try
$rowList[$rowCode] .= "<tr><td class='lens-price-serv'><small>{$lang['services']}:</small> <strong>" . money_format( '%i', $srvPriceTotal ) . "</strong></td></tr>";

How to get a specific part, or div of a website

What I would like to do: get the text headline from the top post on http://reddit.com/r/worldnews and output it to a webpage of mine that will only have that text on it.
In the end, I would like to grab the text from that webpage that I made using AppleScript cURL and output it.
I am making a script that when I click the button it will tell me the top post.
edit If you can think about any way, I would like to do the same thing, but for Facebook notifications.
edit I have PHP grabbing the site and outputting here: http://colejohnsoncreative.com/personal/ai/worldnews.php This is the code that I am using:
<?php
// Get a file into an array. In this example we'll go through HTTP to get
// the HTML source of a URL.
$lines = file('http://www.reddit.com/r/worldnews');
// Loop through our array, show HTML source as HTML source; and line numbers too.
foreach ($lines as $line_num => $line) {
echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";
}
// Another example, let's get a web page into a string. See also file_get_contents().
$html = implode('', file('http://www.example.com/'));
// Using the optional flags parameter since PHP 5
$trimmed = file('somefile.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
?>
So I get all of the site's code to output, but all I need for the project is
<a class="title " href="http://www.dailymail.co.uk/news/article-2219477/Cannabis-factory-couple-gave-400-000-drug-dealing-fortune-poor-Kenyans-jailed-years.html" >British couple who spent most of the money they made from canabis growing on paying for life changing operations and schooling for people in a poor Kenyan village gets sent to prison for 3 years.</a>
and everything else I need to throw away, how can I do that?
If youre in a shell you can wget the page
From php you could file_get_contents the page
From java you could get it with URLConnection
Once you have it, use what ever language you want to look through the text of the page for what you want, and do whatever you like with it
You gonna have to do some parsing. So match the pattern you want. Simplest is to do something like str_pos to get the position of the elements around what you want or use regex.
Do they have a RSS feed? If so you should use that.

Magento/PHP - Modifying the MySQL Queries Underlying the Front-End Product Search Box

Currently, the MySQL database queries that supply the results for the product search field on the front end seem to use "OR" linking criteria in the WHERE clause of the queries.
The reason I assume it is using "OR" is because if you search for something like "green and red plaid shirt", you will get every product with "red" (including "bored", "stored", etc), every product with "green", every product with "plaid", and every product with "shirt".
Now if I can just find out where in the code the queries are being constructed, I should be able to change that to "AND" and end up with queries like this:
SELECT `product_id` FROM `products` WHERE `search_index` LIKE '%red%' AND `search_index` LIKE '%green%' AND `search_index` LIKE '%plaid%' AND `search_index` LIKE '%shirt%';
I haven't been able to find any information by searching Google or Magento's forums. I've been poking around app/code/core/Mage/CatalogSearch/ but have not found the mother lode yet. I know that there is probably some Zend interface I should mess with but haven't found it yet.
Thanks in advance
UPDATE
The below answer does not seem to work for Magento 1.7+, since they've changed some of the search code. I'm working on a solution for that and will update later.
I'm going to answer my own question. Thanks, Anton S for the clues there but I located some key files myself and was able to implement the changes I wanted.
Here is the key file:
app/code/core/Mage/CatalogSearch/Model/Mysql4/Fulltext.php
You would copy the core structure that leads to that file into the local structure, and copy the core file there as well, like so:
app/code/local/Mage/CatalogSearch/Model/Mysql4/Fulltext.php
Then make all changes to the local file, leaving the core file alone.
Look for this bit of code around line 315, inside the function prepareResult($object, $queryText, $query):
foreach($words as $word) {
$like[ ] = '`s`.`data_index` LIKE :likew' . $likeI;
$bind[':likew' . $likeI] = '%' . $word . '%';
$likeI ++;
}
if ($like) {
$likeCond = '(' . join(' OR ', $like). ')';
}
That ' OR ' there is what was giving me thousands of useless results. For example, a search for "green and red plaid shirt" would end up showing me all things green, red, and/or plaid (including shirts, skirts, blimps, rabbits), as well as every single shirt in the store. What the user really wants to find is a product that contains ALL search terms. As noted above, you would also find results like "bored" and "stored" because they contain "red."
To solve most of the problem, you simply have to change that ' OR ' to an ' AND '. Also note that the change only applies to "LIKE" type searches, not "FULLTEXT" type. FULLTEXT doesn't work well in Magento because it excludes way too many results. The method outlined below is much better.
To make the changes:
save the file with the change above.
go into the admin, to System->Catalog->Catalog Search and make sure Search Type is "Like".
Save the configuration
In the admin, go to system->Index Management, and check the box next to Catalog Search Index and reindex it (or just reindex all). (OR from the command line in the magento root type:
php shell/indexer.php --reindex catalogsearch_fulltext
)
If you also want to exclude words like "bored" when searching for "red", then you might want to implement a 2nd change in the same file.
There is another section of code inside that reads:
$bind[':likew' . $likeI] = '%' . $word . '%';
The % at the front means that "bored" is like %red%. But you can't just remove the 1st % to get the right effect because of the way the index is constructed. So instead you make these two changes:
change the above line of code to:
$bind[':likew' . $likeI] = '% ' . $word . '%';
Note the SPACE after the first % before the closing quote. This will now only find words that start with $word (e.g. red, redding, reddy, rediculous all match '% red%'), but you also have to ensure that all words will have spaces before them.
Near the top of the file, under class Mage_CatalogSearch_Model_Mysql4_Fulltext, around line 48 you should find this:
protected $_separator = '|';
I just changed it to this:
protected $_separator = ' | ';
Putting spaces on both sides of the pipe. When you reindex, there will now be spaces before and after every word. A search for "kit" will still give you results for "kitchen", but at least you won't get results for "skit".
Finally, one last change I made was to ensure plural searches return the same results as singular searches, at least for plurals ending in 's'.
I added a line of code where indicated:
foreach($words as $word) {
$word = rtrim($word, 's'); //this line added
$like[ ] = '`s`.`data_index` LIKE :likew' . $likeI;
$bind[':likew' . $likeI] = '%' . $word . '%';
$likeI ++;
}
It simply chops the 's' off the end of every word, so now "red and green plaid shirts" returns the same results as "reds ands greens plaids shirt".
My next project may be to make some more changes to the string parsing to get better results for multi-word searches. I'm looking at this file, fyi: app/code/core/Mage/Core/Helper/String.php
function splitWords
,which is used in the Fulltext.php file for string parsing.
NOTE: To make this change upgrade-safe, you would duplicate the folder structure past app/code/core inside app/code/local, like this:
app/code/local/Mage/CatalogSearch/Model/Mysql4/Fulltext.php
Just copy the core file there, and make your changes there.
You can configure search query's from system > configuration > catalog > catalog search and choose the type of your query's
Search code itself is located under app/code/core/Mage/CataloSearch folder in Mage_CatalogSearch_Model_Query and in Mage_CatalogSearch_Model_Mysql4_Search_Collection class
I am using Magento CE 1.6.0.0 and I found the file in Resource folder rather then the MySql4 folder. Hope this helps.

Categories