Generate iMacro Syntax with PHP - <SP> Spaces - php

I need to generate imacro syntax for the user to be able to copy from the browser screen.
Since iMacro is using <SP> for spaces I need to replace database values that have spaces with with <SP>.
This isn't working because the browser recognizes <SP> as HTML character.
$srtnew = str_replace(" ","<SP>",$string);
Any suggestions?

You can use the HTML entities for angled brackets (< and >):
$srtnew = str_replace(" ","<SP>",$string);
Alternatively, you can use PHP's htmlentities() to encode the brackets:
$srtnew = str_replace(" ",htmlentities("<SP>"),$string);
Working example (PhpFiddle)

You need to do:
header("Content-Type:text/plain");
before you output it.
This tells the browser to output the content as a pure text format and not as html.
Of course you could also do it like this:
$srtnew = str_replace(" ",htmlentities("<SP>"),$string);
If you have many tags you should just set the header if you only want to output that code in order to let your users copy them.
More information about setting headers in PHP: http://www.php.net/manual/de/function.header.php

Related

How to echo hash symbol (#) in PHP?

I have a PHP script that creates an email template, in the template there is a link which contains a URL with a hash symbol (#) to anchor to a certain part of a page.
No matter how I do it, I cannot echo #, it will always convert to %23.
The relevant section of code,
$liURL = 'https://website.com/#hashed-section/#secondary-hash';
echo 'Link';
Try to use html entity:
&num;
Use if the structure, string function, and backslash before sign #.
Just escape it with a backslash:
$liURL = 'https://website.com/\#hashed-section/\#secondary-hash';
If the same code works on other environments, it looks like it's a matter of settings, likely this:
default_charset = "utf-8"
check your php.ini charset on the ones which work as you expect and then check and adjust the one which isn't working.
put it in double quotes
$liURL = "https://website.com/#hashed-section/#secondary-hash";
it will be treated as literal text then

PHP save content from textarea with newline

This is probably really stupid, but I try to save content from a texarea into MySQL with PHP. Normally newlines are preserved in the database. But suddenly they are removed.
I use jquery to send the values to PHP with ajax, and then I do this in PHP:
$var = strip_tags( $_POST["var"] );
$db->query("UPDATE table SET var='$var' WHERE id=$id")
Somehow newlines are lost on the way in. If I do nl2br on the var, then they are translated to <br/>, so $var contains newline right up until I run the query.
Update, to add to the strangeness. If I actually run nl2br on $var, and then replace br-tags with newline, before updating the table, all is well, what is going on?!?
This is working just fine:
$var = strip_tags( $_POST["var"] );
$var = nl2br($var);
$var = preg_replace('#<br\s*/?>#i', "\n", $var);
$db->query("UPDATE table SET var='$var' WHERE id=$id");
I've had another look into the Zebra (source code this time), and as mentioned in previous comment this is exactly why newlines get removed. The escape method from Zebra package uses:
http://php.net/manual/en/mysqli.real-escape-string.php
which means the following are removed:
NUL (ASCII 0), \n, \r, \, ', ", and Control-Z.
You could try to add the input in:
<pre></pre>
This will save your input as preformatted text in the database.
Reference: https://www.w3.org/wiki/HTML/Elements/pre
nl2br is a good solution too, use whichever you prefer.
Also make sure you are using the right configuration for your database column. It should be VARCHAR or TEXT.
I think your problem is that newlines get removed before being passed to the server.
You mentioned it's being sent with jQuery so make sure there isn't any client-side processing before being sent to the server.

How to change encoding from plain text to Unicode so that I can read special characters from a HTML?

Below is my code :
<?php
// example of how to use basic selector to retrieve HTML contents
include('/Library/WebServer/Documents/simple_html_dom.php'); //this is the api for the simplehtmldom
// get DOM from URL or file
$html = file_get_html('http:/www.google.hk');
// extract text from table
echo $html->find('td[align="top"]', 1)->innertext.'<br><hr>';
// extract text from HTML
echo $html->innertext;
?>
I am using the simplephphtmldon API. When I execute my php program in my local server instead I get so many unrecognized characters due to the fact that the plain text can't really encode them to show up like they supposed to. Can Someone tell me what i need to change to inner text in order to get all the characters to show up? PS i also did try plaintext without any luck. textContent seems broken to me. Perhaps i need to try a different element first (?). Thanks
echo utf8_encode($html->innertext);
Or
echo utf8_decode($html->innertext);
It depends on the original encoding, so you may want to try both.
Note:
If you're seeing the output on a browser, make sure you set Unicode as text encoding or use this the following code at the top of you script.
header('Content-Type: text/html; charset=utf-8');

PHP string cut short

Why does this code
$string = "!##$%^&*(<a#g.com";
echo $string;
only output:
!##$%^&*(
Is this is a PHP bug?
Because < is a reserved character in in HTML :)
Use < and >
Read this for more information
http://www.w3schools.com/HTML/html_entities.asp
You can use the function htmlspecialchars to convert such special chars
http://php.net/manual/en/function.htmlspecialchars.php
I'm not seeing that:
http://ideone.com/zhycx
Perhaps you've got some weird characters in your file? Make sure you're using a "normal" encoding on your source code, as well.
You need to do:
echo htmlentities($string);
to display the string as it is on a browser. This is because the < in the string is interpreted by the browser as start of a HTML tag.
So it's not PHP but the browser that is causing this behavior. If you do the exact same display on a command line, you'll see all the characters.
If you are viewing the output in a web browser, then the < begins a tag and is usually not displayed but interpreted in the HTML document structure parser. Also, a $ inside of a double-quoted string is interpolated as the variable name that follows it; try using single quotes where this won't happen.
Try this:
$string = '!##$%^&*(<a#g.com';
echo htmlentities($string);

html source encode

when I view source on my php page I get " for a quote. But instead, I would like " to be used in the source code. I have no control over manually replacing it so Im wondering if there is a function to do such a thing.
If you have access to the PHP and want to change all html special characters to their rightful variations use:
print htmlspecialchars_decode($string);
You could do this very simply using str_replace.
$string = str_replace('"', '"', $string);
However, as Levi said, why not just leave it this way? It should have no effect on the display.

Categories