How to concatenate the single and double quote - php

I am having a problem in concating the PHP variable , have a look below
$attributes .= ' href="javascript:bookmarkscroll.scrollTo('.$varpost->post_name.')"';
I want the output like
javascript:bookmarkscroll.scrollTo('about-us');
Thanks All.

Just escape your quotes inside the js function,
$attributes .= ' href="javascript:bookmarkscroll.scrollTo(\''.$varpost->post_name.'\')"';

Here you go:
$attributes .= ' href="javascript:bookmarkscroll.scrollTo(\''. $varpost->post_name .'\')"';

Use json_encode. Keep in mind that JSON-encode will only escape " (not ') so make sure your output HTML uses only '.
$js = 'javascript:bookmarkscroll.scrollTo("' . $varpost->post_name . '");';
$attributes .= ' href="' . json_encode($js) . '"';
Ideally you'd use a more reliable way to generate the variable parts of JavaScript. In this case you would still use json_encode.

Related

PHP: how to add double quote to a PHP variable

When i echo "$time"; - The output is 2015-07-27 18:17:47
But i need to output as "2015-07-27 18:17:47".
I have been trying various string concatenations such as : echo "."$time"."; But couldn't get the desired output? What is the best way to do it?
Try this concatenation
echo '"' . $time . '"';
or use printf() like so
printf('"%s"', $time);
Just escape them:
echo "\"$time\"";
You could also use single around the double quotes:
echo '"' . $time . '"';
See here for more info on escape sequences when using double quotes.

Print a space in PHP

Im echoing some data from a database using PHP. However the data is too close together and needs a space in between each one.
while($book = mysql_fetch_array($books)) {
echo '<div>'
.$book['title']
.$book['author']
.$book['genre']
.$book['price']
.$book['availability']
.'</div>';
}
Is their a way to print a break maybe after each one to give a space.
Cheers
You can print it as html entity :
echo '<div>'
.$book['title'] . ' '
.$book['author'] . ' '
.$book['genre'] . ' '
.$book['price'] . ' '
.$book['availability']
. '</div>';
yes, and the answer is shown below
while($book = mysql_fetch_array($books)) {
echo '<div>'
.$book['title']." "
.$book['author']." "
.$book['genre']." "
.$book['price']." "
.$book['availability']." "
.'</div>';
}
To add my five cents )
while($book = mysql_fetch_array($books)) {
echo "<div>{$book['title']}
{$book['author']}
{$book['genre']}
{$book['price']}
{$book['availability']}</div>";
}
You may use the curly syntax in the double quoted strings:
echo "<div>{$book['title']} {$book['author']} {$book['genre']} {$book['price']} {$book['availability']}</div>";
When a string is specified in double quotes or with heredoc, variables
are parsed within it.

php string concatenation "A<"."B" does not work

I'm writing a function to output HTML elements, the problem is: when I try to concatenate this two strings:
$tag = "<" . "tag";
The instruction echo $tag outputs nothing. What is wrong
As mentioned in comments, special characters like <, will be parsed by browser as HTML, therefore you won't see them as you expect.
Its almost the same thing:
$tag = 'p';
echo '<' . $tag '>' . Test . '</' . $tag . '>';
Which is the same as
echo '<p>' . Test . '</p>';
So after script execution you'll see just
Test
in a browser. but when viewing a source, it will be as
<p>Test</p>
If for some reason you want to see HTML tags, then you need to escape special chars using built-in function htmlentities().
In your case, you can just prepare a string, then just echo it like
echo htmlentities($string);
If by tag you mean an HTML entity then its not going to be seen in the browser. You may need to do a 'view source' to see what was created by echo call.

apostrophe php issue

I was making a school assignment with involves a shoutbox. A found great tutorial wich uses jquery,ajax,mysql and php. Now i run into a little problem with the following sentence:
$result .= "<li><strong>".$row['user']."</strong><img src="\" alt="\"-\"" />".$row['message']." <span class="\"date\"">".$row['date']."</span></li>";}
I was wondering if anybody could find out why it gives errors. So far I came to this conclusion $row['message'] and then it thinks the rest of the code as a string. So it probably is a apostrophe problem.
Just for the sake of making your life easier: use ' for the php and " for html like this:
$result .= '<li><strong>'.$row['user'].'</strong><img src="" alt=""/>'.$row['message'].' <span class="date">'.$row['date'].'</span></li>';
Pretty sure you should get the idea.
$result .= "<li><strong>{$row['user']}</strong><img src='http://www.' alt='My Alt Tag' />{$row['message']}<span class='date'>{$row['date']}</span></li>";
You're confusing yourself by coming in and out of quotations - you can wrap variables with {} to force the interpolation in such cases.
$result .= "<li><strong>".$row['user']."</strong><img src='' alt='-'/>".$row['message']." <span class='date'>".$row['date']."</span></li>";}
Avoid using " inside of the string - it is easy to forget about escaping special chars. Instead of " use '.
Besides - you use " only when there is any PHP parsing necessary within this string. E.g.
$var1 = 1;
$test = "$var1"; //evaluates to '1'
$test = '$var1'; //evaluates to '$var1'
It appears that you are attempting to escape quotes and making your job harder. A great feature in PHP for HTML output is using quoted strings so that you don't have to worry about escaping double quotes. Please reference the PHP Manual for Strings.
In other words your line becomes:
$result .= '<li><strong>' . $row['user'] . '</strong><img src="" alt="-" />' . $row['message'] .
'<span class="date">' . $row['date'] . '</span></li>' .
'<li><strong>' . $row['user'] . '</strong><img src="" alt="-" />' . $row['message'] .
'<span class="date">' . $row['date'] . '</span></li>';

Quotes problem when passing value to Javascript

I am using like
$myPage .= '<td><a href=\'javascript:editProduct('
.$row['id']
.',"'
.$row['name']
.'")\'>Edit</a></td>';
where $row['name'] has quotes in its value. it breaks. how do i solve the issue both from php side and js side...
$row['name'] is value from DB. and it will have value like pradeep's and pradeep"s also
i used like
$myPage .= '<td><a href=\'javascript:editProduct('.addslashes($row['id']).',"'.addslashes($row['name']).'")\'>Edit</a></td>';
it solves the issue of double quotes. but when i have single quotes in value the javascrit link looks like
javascript:editProduct(28,"pradeep\
it actually breaks..
And how do i strip down the slashes added by addslashes in javascript..
UPDATE - FINAL CODE
$myPage .= '<td><a href=\'javascript:editProduct('.$row['id'].',"'.htmlentities($row['name'],ENT_QUOTES).'")\'>Edit</a></td>';
and js looks like
function editProduct(id,name){
alert(name);
}
can any one solve my issues
Try:
$myPage .= "<td><a href='javascript:editProduct({$row['id']},\""
. htmlentities( $row['name'] )
. "\")'>Edit</a></td>";
htmlentities default behaviour is to convert double quotes and leave single quotes alone, if you require converting single and double quotes, then call it like this:
htmlentities( $row[ 'name' ], ENT_QUOTES )
Also, using { .. } in "..." strings is the correct way to substitute variables.
The PHP string
'<a href=\'javascript:editProduct('.$row['id'].',"'.$row['name'].'")\'>';
outputs (assuming some values)
<td><a href='javascript:editProduct(123,"abc")'></td>
Presumably it breaks if $row['name'] contains a " quote. You could replace such quotes with a \" in the string before you output it using str_replace('"', '\"', $row['name'])

Categories