I'm now doing it this way:
<a title="<?php echo $title; ?>">...
But it will brreak when " is included in $title.
Not that it's "the final solution", but obviously you need to escape any literal string that isn't mean to contain HTML. In this case:
<a title="<?php echo htmlspecialchars($title); ?>">
You should run that through htmlspecialchars first to make sure your HTML won't break.
You should translate special characters into HTML entities first, easily done with htmlentities().
<a title="<?php echo htmlentities($title); ?>">
Related
I implemented text switching system as portrayed here http://www.bitrepository.com/php-how-to-add-multi-language-support-to-a-website.html .
Every link is defined like so:
<li><?php echo $lang['MENU_REGISTER']; ?></li>
By this logic, adding a title/description to a link would work like so:
<a title=<?php echo $lang['MENU_REGISTER']; ?> href="#">
<?php echo $lang['MENU_REGISTER']; ?></a>
I'm under impression that this does not work the way as I wish it would.
Any suggestions?
You are missing opening and closing quotes:
title="<?php echo $lang['MENU_REGISTER']; ?>"
and preferably addslashes for other quotes:
title="<?php echo addslashes($lang['MENU_REGISTER'];) ?>"
I have a problem to add space and comma between two variables:
<td><?php echo '<a href=https://www.google.de/maps/place/
'.$row['ort'].$row['strasse'].' target="_blank">'.$row['ort'].'</a>';?></td>
The results is
BerlinKolnerweg
but I want to insert space and comma like this:
Berlin, Kolnerweg
This does not work:
.$row['ort'].', ' .$row['strasse'].
Can you help me?
I added a space (escaped as %20) and a comma in the url. I moved the php into the anchor-tag instead of echoing out the whole thing. Try if this works:
<a href="https://www.google.de/maps/place/<?php echo $row['ort'].',%20'.$row['strasse']; ?>" target="_blank">
<?php echo $row['ort'].', '.$row['strasse']; ?>
</a>
Try this:
<td>
<a href=https://www.google.de/maps/place/<?php echo $row['ort'].$row['strasse']; ?>' target="_blank">
<?php echo $row['ort'].", ".$row['strasse']; ?>
</a>
</td>
Hope this helps.
So I'm using the custom pinterest button builder and it spits out and anchor that looks like this:
<a href="//gb.pinterest.com/pin/create/button/?url=http%3A%2F%2Fwww.flickr.com%2Fphotos%2Fkentbrew%2F6851755809%2F&media=http%3A%2F%2Ffarm8.staticflickr.com%2F7027%2F6851755809_df5b2051c9_z.jpg&description=Next%20stop%3A%20Pinterest" data-pin-do="buttonPin" data-pin-config="none">
The url is composed of a few parameters, which are url, media and description.
I want to replace each parameter with my own, using php in wordpress.
I'd like something along the lines of this:
<a href="//gb.pinterest.com/pin/create/button/?url=<?php echo $url; ?>&media=<?php echo $url; ?>&description=<?php the_title(); ?>" data-pin-do="buttonPin" data-pin-config="none">
If you look at pinterests example the url and description are encoded to replace spaces and special characters with %20 or other % type replacements.
So, how do I encode a url like:
http://lart.co.uk/wordpress/wp-content/uploads/2014/03/30d5466ca31411e3863812695239df89_8.jpg
And text like:
This is a description
so pinterest will accept it?
Use urlencode(). Pay attention to the parameters in the_title():
<a href="//gb.pinterest.com/pin/create/button/?url=<?php echo urlencode($url); ?>&media=<?php echo urlencode($url); ?>&description=<?php echo urlencode(the_title(null,null,false)); ?>" data-pin-do="buttonPin" data-pin-config="none">
I have php file index.php
In this file to use html code I am doing:
index.php
echo '
<div>
<a class="fragment" href="">
<div>';
In href I want to put value of some php variable i.e. $url How could be done?
is this correct way?
<a class="fragment" href="<?php echo $url; ?>">
You concatenate the string by ending it and starting it again:
echo '
<div>
<a class="fragment" href="' . $url . '">
<div>';
Though I personally prefer to stop the PHP tags and start them again (if I have a lot of HTML) as my IDE won't syntax highlight the HTML as it's a string:
?>
<div>
<a class="fragment" href="<?php echo $url; ?>">link</a>
</div>
<?php
Since you are printing several lines of HTML, I would suggest using a heredoc as such:
echo <<<HTML
<div>
<a class="fragment" href="$url">
<div>
HTML;
HTML can be anything as long as you use the same tag both in the beginning and the end. The end tag must however be on its own line without any spaces or tabs. With that said, specifically HTML also has the benefit that some editors (e.g. VIM) recognise it and apply HTML syntax colouring on the text instead of merely coluring it like a regular string.
If you want to use arrays or similar you can escape the variable with {} as such:
echo <<<HTML
<div>{$someArray[1]}</div>
HTML;
if you are echoing php directly into html i like to do this
<div><?=$variable?></div>
much shorter than writing the whole thing out (php echo blah blah)
if you are writing html in php directly then there several options
$var = '<div>'.$variable.'</div>'; // concatenate the string
$var = "<div>$variable</div>"; // let php parse it for you. requires double quotes
$var = "<div>{$variable}</div>"; // separate variable with curly braces, also requires double quotes
Do it like
<?php
$url='http://www.stackoverflow.com';
echo "<div><a class='fragment' href='$url' /></div>";
If you want to maintain the echo statement you can do either
echo '<a class="fragment" href="'.$url.'"><div>';
or
echo "<a class=\"fragment\" href=\"$url\">";
The first is better for performances and IMHO is more readable as well.
If you want to input/output large blocks of HTML with embedded variables you can simplify the process by using Heredocs:
echo <<<_EOI_
<div>
<a class="fragment" href="$url">
<div>
_EOI_;
You don't have to worry about escaping quotes, constant concatenation, or that ugly dropping in and out of <?php echo $var; ?> that people do.
I want to echo the following in PHP:
echo "<td><p style='visibility: hidden' id='joindata$rowindex'>$joindata</p><a style='visibility: visible;' onclick='toggleDisplay('joindata$rowindex'); toggleDisplay('showjoindata$rowindex')' id='showjoindata$rowindex'>Show</a></td>";
But it is not echoing due to the nested ' ' Any Ideas how I can echo HTML elements which have events which call javascript functions which have parameters in PHP would be much appreciated, thanks :)
Just use double-quotes to delimit your attribute values, and escape them in your PHP string:
echo "<td><p style=\"visibility: hidden\" id=\"joindata$rowindex\">$joindata</p><a style=\"visibility: visible;\" onclick=\"toggleDisplay('joindata$rowindex'); toggleDisplay('showjoindata$rowindex')\" id=\"showjoindata$rowindex\">Show</a></td>";
Or, you could separate behaviour from content and just add the event handlers dynamically.
You have to add some double quotes, Try this:
echo "<td><p style=\"visibility: hidden\" id=\"joindata".$rowindex."\">$joindata</p><a style=\"visibility: visible;\" onclick=\"toggleDisplay('joindata".$rowindex."'); toggleDisplay('showjoindata".$rowindex."')\" id=\"showjoindata".$rowindex."\">Show</a></td>";
PHP have best performance and scalability when you separate the views from the data. However in this case you could do:
<td>
<p style="visibility: hidden" id="joindata<?php echo $rowindex; ?>">
<?php echo $joindata; ?>
</p>
<a style="visibility:visible" onclick="toggleDisplay('joindata<?php echo $rowindex; ?>'); toggleDisplay('showjoindata<?php echo $rowindex; ?>')" id="showjoindata<?php echo $rowindex; ?>">Show</a>
</td>
Also take a look on the jQuery docs to avoid hard-coding the javascript functions. Your app will run faster.