什么是实现title =“<?php echo $ title?>”的完美解决方案[关闭]

My current solution will suck sometimes

EDIT

For those who don't understand,see this example:

<a title="<?php echo 'test';?>" >...

Try this:

<a title="<?php echo htmlentities($title); ?>">

You can use a template engine, like Smarty.

But I prefer my own little tricks. I actually use globals, in a config file where I setup site wide variables, and constants.

e.g.

$title = 'My site';
$keywords = 'my site';
$description = 'welcome';
$javascript = array('jquery.js', ...);
$css = array('default.css');

define('SITE_URL', 'http://localhost/');
define('ABSPATH', dirname(__FILE__));
...

My template will write out a HTML using the above variables.

<a title="<?php echo 'test'; ?>">

Is that what you're looking for?

I can only give general information with that little. The best I can say is that if you're inserting PHP variable values into HTML, you probably want to be outputting using the built-in templating.

echo <<<TEMPLATE
<a href="http://stackoverflow.com" title="{$title}">Stackoverflow</a>
TEMPLATE;

This is much more clear and easier to manage.

Since you're outputting to HTML, you need to convert special characters to HTML entities so you won't have any collisions with the HTML and your actual content.

In your example, (I think) you're trying to say that <a title="<? echo 'test"'; ?>">, and there would be a problem because of the extra quotation marks.

So you simply convert them to the proper &quot;, with the PHP function htmlspecialchars:

<a title="<? echo htmlspecialchars('test"'); ?>">