When using Abrahams/TwitterOAuth library, special HTML characters are placed in a Tweet.
$content = "test's";
$params = array(
"status" => $content
);
$result = $connection->post("statuses/update", $params);
results in
test's
within the posted Tweet status.
A vardump($content) in the browser reveals test's as expected.
I've tried html_entity_decode and htmlspecialchars_decode but neither work for the posted Tweet.
You need to tell it explicitly to convert the quotes. This is done with ENT_QUOTES.
Example:
echo htmlspecialchars_decode('test's',ENT_QUOTES) . "\n";
echo htmlspecialchars_decode('test's');
Output:
test's
test's
Demo: https://eval.in/426992
There are also other constants that can be used if you only want to decode single quotes etc. See the manual for more information, http://php.net/manual/en/function.htmlspecialchars-decode.php.
ENT_QUOTES Will convert both double and single quotes.
$content = "test's";
$content = htmlspecialchars_decode($content, ENT_QUOTES);
works for me.
Thanks chris85!
Related
i am trying to use echo inside url. i have store data from the form in database and now i am also fetching it on my page and its working well. Now i am trying to print that data i.e. number and date in url.
Is it possible and if possible please help me out
here is my data that i am fetching and it prints the output
echo $number;
echo $yyyymmdd;
and here is my url in which i want to insert ' echo $number; ' and ' echo $yyyymmdd; ' on the place of and .
$json= file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/<number>/date/<yyyymmdd>/");
I have also tried something like this but it gives error of syntex error.
$json= file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/"echo $number;"/date/"echo $yyyymmdd;"/");
Another way to add changing parameters to a URL (or string) is by using sprintf(). You define your URL and a type specifier like %d as a placeholder for numbers, and %s for strings. See the php doc for the full list of type specifiers.
$urlFormat = "http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/%d/date/%s/"
^ ^
Then call sprintf with the changing parameters in order of appearance.
$url = sprintf($urlFormat, $number, $yyyymmdd);
$json = file_get_contents($url);
This becomes more convenient especially if you are calling file get contents in a loop.
Create two variables and append those two inside double-quote or single quote, depending upon the quotes which you have opened and close it.
<?php
$number=123;
$yyyymmdd='2018-10-9';
$json= file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/".$number."/<number>/date/<".$yyyymmdd.">/");
?>
$json= file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/".$number."/date/".$yyyymmdd."/");
When you compose text, you do not need "echo" but just can write variable.
You can directly use variables in double quotes like this
file_get_contents("http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/$number/date/$yyyymmdd/");
Sample code below
$number = 344;
$yyyymmdd = "20180301";
$url1 = "http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/$number/date/$yyyymmdd/";
echo "url1 ".$url1."\n";
$url2 = "http://api.com/api/a2/live/apikey/fc5a69f870fdb03/number/".$number."/date/".$yyyymmdd."/";
echo "url2 ".$url2. "\n";
I know this question has been asked before but the answers are not the solution to my problem.
When i post a text with this code:
$info=html_entity_decode(mysql_real_escape_string($_POST['info']));
Like :
fdsa
fdsa
fasf
and when i posted this text with antered and spaces it looks like this
<?php echo $info;?>
fdsa\r\nfdsa\r\nfasf\r\n
i try this nl2br($info) but still not working.
What do I need to appear in the text in this way?
fdsa
fdsa
fasf
Replace the \r\n by <br>, the \n by <br>, then the\r by <br>:
$info = html_entity_decode($_POST['info']);
$info = str_replace('\r\n' , '<br>', $info);
$info = str_replace('\n' , '<br>', $info);
$info = str_replace('\r' , '<br>', $info);
$info = html_entities($info);
echo $info;
You have to make multiples replacements since new lines can be represented differently according to the operating system (See this page for more details)
Finally, sanitize the value with html_entities before echoing it, preventing client side attacks.
EDIT : Removed the mysql_... function, not needed (the value isn't intended to be inserted in a MySQL database, not now at least).
Also, read Lashus advice bellow and apply it ;)
I got a problem with single quotes in a regular expression.
What i want to do is replace smileys in a string to a html image tag.
All smileys are working, except the sad smiley :'-( because it has a single quote in it.
Magic Quotes is turned off (testet with if (g!et_magic_quotes_gpc()) dd('mq off');).
So, let me show you some code.
protected $emoticons = array(
// ...
'cry' => array(
'image' => '<img class="smiley" src="/image/emoticon/cry.gif" />',
'emoticons' => array(":'(", ";'(", ":'-(", ";'-(")
),
);
My method to replace all the emoticons is the following:
public function replaceEmoticons($input) {
$output = $input;
foreach ($this->emoticons as $emo_group_name => $emo_group) {
$regex_emo_part = array();
foreach ($emo_group['emoticons'] as $emoticon) {
$regex_emo_part[] = preg_quote($emoticon, '#');
}
$regex_emo_part = implode('|', $regex_emo_part);
$regex = '#(?!<\w)(' . $regex_emo_part .')(?!\w)#';
$output = preg_replace($regex, $emo_group['image'], $output);
}
return $output;
}
But as i said: ' kills it. No replacement there. :-) :-/ and so on are working. Why?
FYI Content of $regex: #(?!<\w)(\:\'\(|;\'\(|\:\'\-\(|;\'\-\()(?!\w)#
What is wrong here, can you help me?
UPDATE:
Thanks # cheery and cychoi. The replacing method is okay, you've got right.
I found the problem. My string gets escaped before it is forwarded to the replaceEmoticons method. I use TWIG templating engine and i use |nl2br filter before my selfmade replace_emoticon filter.
Let me show you. This is the output in the final template. It is a template to show a comment for an blog entry:
{{ comment.content|nl2br|replace_emoticons|raw }}
Problem: nl2br is auto pre-escaping the input string, so ' gets replaced by the escaped one '
I need this nl2br to show linebreakes as <br /> - and i need the escaping too, to disallow html tags in the user's input.
I need replace_emoticons to replace my emoticons (selfmade TWIG extension).
And i need raw here at the end of the filter chain too, otherwise all HTML smiley img tags gets escaped and i will see raw html in the comment's text.
What can i do here? The only problem here seems to be that nl2br escapes ' too. This is no bad idea but in my case it will destroy all sad smileyss containing ' in it.
Still searching for a solution to solve this and i hope you can help me.
Best,
titan
I added an optional parameter to the emoticon method:
public function replaceEmoticons($input, $use_emo_encoding_for_regex = true) {
and i changed the foreach part a lil' bit:
foreach ($emo_group['emoticons'] as $emoticon) {
if ($use_emo_encoding_for_regex === true) {
$emoticon = htmlspecialchars($emoticon, ENT_QUOTES);
}
$regex_emo_part[] = preg_quote($emoticon, '#');
}
It works! All emoticons are replaced!
I am having issues with this code
This is my PHP that worked with other projects...
$gas = file_get_contents('http://api.mygasfeed.com/stations/loadbygeo/47.9494949/120.23423432/reg|mid|pre|diesel/'. $api . '.json?callback=?');
$json_output = json_decode(utf8_decode($gas));
$location= $json_output->geoLocation->city_id;
This is the JSON result
?({"status":{"error":"NO","code":200,"description":"none","message":"Request ok"},"geoLocation":{"city_id":"13123","city_long":"Hulunber","region_short":"Nei Mongol","region_long":"Nei Mongol","country_long":"China","country_id":"49","region_id":"6010"},"stations":[]})
This code is returning a blank result.
You need to remove the ?( and the ). See WP:JSONP.
One could use substr() for that, or better yet a regex to assert and remove only the desired garbage:
$json = preg_replace("/ ^[?\w(]+ | [)]+\s*$ /x", "", $jsonp);
Would remove arbitrary callbackFnNames( but also your strange ?( pseudo function call.
Is there a helper function that will properly escape a string to be rendered as a single quote quoted JavaScript string literal?
I know of jsQuoteEscape but it only handles quotes and does not treat \n & \r etc.
so if my string is 'line1\nlineb' (i.e. two lines with a newline between them)
and I use
var jsvar='<?php echo $this->helper('myextension')->jsQuoteEscape($mystring); ?>';
I will get in the rendered content
var jsvar='line1
line2';
which is a syntax error.
Thanks,
Eyal
Yes
$string = 'Hello
There';
var_dump( Mage::helper('core')->jsonEncode($string) );
var_dump( json_encode($string) );
I've never been clear if this encoding a non-object string datatypes as a javascript string is a side-effect of the JSON encoding, or if it's true, according to Hoyle Crockford JSON, so I always like to wrap my strings in an object when passing them around
$o = new stdClass();
$o->param = 'This is my
Param';
$json = json_encode($o);
echo 'var data = ' . $json . ';' . "\n";
echo 'var jsdata = data.param';
This is how you'd handle this with javascript. There's no method that's build specifically for this. If you're interested in seeing the helper methods you do have available from a block, checkout the methods in
app/code/core/Mage/Core/Block/Abstract.php
app/code/core/Mage/Core/Block/Template.php
and if you're dealing with a template that's part of a block higher up the chain, get its class and then check its definition
var_dump get_class($this);