PHP Missing Slashes - php

I am convinced that the solution is very simple, however I have searched many pages and questions and I am slightly frustrated with the lack of solution.
I'm trying to create a span element which has the onclick property.
The onclick event should pass to the displayStory function path to the text file.
Unfortunately, all slashes are missing.
Slashes are fine if I dont use apostrophes inside $dir, however i have to insert them somehow
PHP:
<?php
$allStories = scandir("./stories");
foreach($allStories as $story){
$dir = "'/stories/$story'";
$element = ("<span class='listElement' onclick='displayStory($dir)'>$story</span>");
echo $element;
}
?>
Output:
<span class="listElement" onclick="displayStory(" stories="" example.txt')'="">example.txt</span>

You need to learn how to escape characters:
<?php
$allStories = scandir("./stories");
foreach($allStories as $story){
// No single quotes here
$dir = "/stories/$story";
// No parenthesis needed to assign a value
// Escape double quotes for attributes preceding them with a backslash \"
// Use single quotes for function parameter
$element = "<span class=\"listElement\" onclick=\"displayStory('$dir');\">$story</span>";
echo $element;
}
?>
Output should be:
<span class="listElement" onclick="displayStory('stories/example.txt');">example.txt</span>

Related

Escaping quotes in PHP with variable

There are numerous question on Stackoverflow concerning this but I have not been able to solve this still.
I'm trying to put multiple variables inside global attribute data-*. The output should be:
data-info="23;thisWeek"
The 23 comes from a database: $row["id"], and thisWeek from a variable: $categori1.
I've tried:
echo "<tr data-info=" .fixSlashes($row["id"], $categori1); " class=\"tableclass\"><td>"
Then
<?php
function fixSlashes($idP, $categoriP){
$str = addslashes($idP . ";" .$categoriP);
return $str;
}
?>
But something goes wrong. No error message, but the behaviour is wrong. It works if I just write:
echo "<tr data-info="data-info="23;thisWeek" class=\"tableclass\"><td>"
It looks like you are not outputting the html correctly. Note the escaped quotes around the fix Slashes function. Also note the "." instead of the semicolon.
"<tr data-info=\"" .fixSlashes($row["id"], $categori1) . "\" class=\"tableclass\"><td>"
If you want to see all errors include the following code. This would have caught the semicolon error.
error_reporting(E_ALL);
ini_set('display_errors', '1');
echo "<tr data-info=" .fixSlashes($row["id"], $categori1); " class=\"tableclass\"><td>"
You have a semicolon (;) where the concat operator (.) should be. Umm, after the fixSlashes function. Before the class suffix string.
You're ending the echo statement prematurely, before the "class=\"tableclass\">" can be appended.
I hate to piggy-back, but half the answers are getting the one problem and half the answers are getting the other problem.
You need to fix your semi-colon to a period (concat) as #MingShun said.
You need to get quotes (escaped) around your data as #JimmyScray said.
echo "<tr data-info=\"" .fixSlashes($row["id"]. $categori1). "\" class=\"tableclass\"><td>";
Use escape sequence for displaying double quotations and use single quotations for id (i.e $row['id'])
echo "<tr data-info=\" $row['id'];$categori1\" class=\"tableclass\"><td>";

PHP Regular Expression - Single Quote not working - TWIG pre-escaping

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!

PHP read and execute contents of a file as a string

I am trying to read a file as a string and return it to the client to be executed as javascript code in order to save it as a variable. Like so
<?php
$fileName = 'target.js';
$codeAsString = file_get_contents($fileName);
$script = 'var code=\'' . $codeAsString . '\'';
echo $script;
?>
Once returned, I want the variable code to have a string representation of the contents of target.js. However, it isn't working. I suspect it has to do with new line characters and single/double quotes... In Python they have a multi line quote
"""This
is
a "multi"
line
'quote'
"""
Is there anything like this in Javascript/php? I can't even wrap my head around whether I need the single quotes around $codeAsString when appending it to $script. Do I have to manually go in and prepend backslashes before all quotes, double quotes, back slashes...Surely they have helper functions for this
thanks.
json_encode is your friend...
<?php
$fileName = 'target.js';
$codeAsString = file_get_contents($fileName);
$script = 'var code= '.json_encode($codeAsString) .';';
echo $script;
?>
Use PHP function json_encode.

php check if file exist, however it includes a lot of apostrophes and backslashes

I was wondering what you'd do if you wanted to link to a directory index file if it existed?
So far what I have is this
$filename = './../season/6/index.php';
if (file_exists($filename)) {
echo '<div id="lesson">
season 6
</div>';
} else {
echo "";
}
However, what I want now is something like this
$lesson="5";
$lesson++; // (or someway to increase it)
$filename = './../season/$lesson/index.php';
if (file_exists($filename)) {
echo '
season $lesson
';
} else {
echo "";
}
however, I know that php won't allow ALL Those backslashes in that echo or apostrophe. How can I compensate? Should I use String concatenation?
As an example:
<?php
foreach(range(1,10) as $lesson){
$filename = "./../season/$lesson/index.php";
echo $filename;
}
?>
A live demo:
http://www.ideone.com/pemRv
Essentially two things here:
1) Use double quotes to have variable interpolation.
2) Just have $lesson be an integer, so you can increment the value.
Variable interpolation only occurs in double quoted strings, you are using single quoted.
Also, your else { ... } is unnecessary.
However, it sounds like this method could be improved. What upper bound will you go to before you stop checking? This will be a lot of unnecessary overhead.

Replacing words with tag links in PHP

I have a text ($text) and an array of words ($tags). These words in the text should be replaced with links to other pages so they don't break the existing links in the text. In CakePHP there is a method in TextHelper for doing this but it is corrupted and it breaks the existing HTML links in the text. The method suppose to work like this:
$text=Text->highlight($text,$tags,'\1',1);
Below there is existing code in CakePHP TextHelper:
function highlight($text, $phrase, $highlighter = '<span class="highlight">\1</span>', $considerHtml = false) {
if (empty($phrase)) {
return $text;
}
if (is_array($phrase)) {
$replace = array();
$with = array();
foreach ($phrase as $key => $value) {
$key = $value;
$value = $highlighter;
$key = '(' . $key . ')';
if ($considerHtml) {
$key = '(?![^<]+>)' . $key . '(?![^<]+>)';
}
$replace[] = '|' . $key . '|ix';
$with[] = empty($value) ? $highlighter : $value;
}
return preg_replace($replace, $with, $text);
} else {
$phrase = '(' . $phrase . ')';
if ($considerHtml) {
$phrase = '(?![^<]+>)' . $phrase . '(?![^<]+>)';
}
return preg_replace('|'.$phrase.'|i', $highlighter, $text);
}
}
You can see (and run) this algorithm here:
http://www.exorithm.com/algorithm/view/highlight
It can be made a little better and simpler with a few changes, but it still isn't perfect. Though less efficient, I'd recommend one of Ben Doom's solutions.
Replacing text in HTML is fundamentally different than replacing plain text. To determine whether text is part of an HTML tag requires you to find all the tags in order not to consider them. Regex is not really the tool for this.
I would attempt one of the following solutions:
Find the positions of all the words. Working from last to first, determine if each is part of a tag. If not, add the anchor.
Split the string into blocks. Each block is either a tag or plain text. Run your replacement(s) on the plain text blocks, and re-assemble.
I think the first one is probably a bit more efficient, but more prone to programmer error, so I'll leave it up to you.
If you want to know why I'm not approaching this problem directly, look at all the questions on the site about regex and HTML, and how regex is not a parser.
This code works just fine. What you may need to do is check the CSS for the <span class="highlight"> and make sure it is set to some color that will allow you to distinguish that it is high lighted.
.highlight { background-color: #FFE900; }
Amorphous - I noticed Gert edited your post. Are the two code fragments exactly as you posted them?
So even though the original code was designed for highlighting, I understand you're trying to repurpose it for generating links - it should, and does work fine for that (tested as posted).
HOWEVER escaping in the first code fragment could be an issue.
$text=Text->highlight($text,$tags,'\1',1);
Works fine... but if you use speach marks rather than quote marks the backslashes disappear as escape marks - you need to escape them. If you don't you get %01 links.
The correct way with speach marks is:
$text=Text->highlight($text,$tags,"\\1",1);
(Notice the use of \1 instead of \1)

Categories