简体   繁体   English

使用 PHP 根据用户输入构建 HTML 链接

[英]Building HTML links based on user input with PHP

I am wanting to be able to create links that show up on the links.html page based on user submissions.我希望能够根据用户提交的内容创建显示在links.html页面上的链接。

The links would follow this format <a href="URL">TITLE</a> , so quite simplistic.链接将遵循这种格式<a href="URL">TITLE</a> ,因此非常简单。

Users will submit data via this form:用户将通过此表单提交数据:

 <form action="links.php" method="post"> <input type="text" placeholder= "URL:" name="url" required><br> <input type="text" placeholder= "Title:" name="title" required><br> <input type="submit">

And the PHP I'm using is我正在使用的 PHP 是

<?php
$url = $_POST["url"];
$title = $_POST["title"];
$text = "<a href="".$url.">".$title."</a> <br> \n"
$file = fopen("./data/links.html","a+ \n");
 fwrite($file, $text);
 fclose($file);
?>

I know that the issue lies with building the ".$url."我知道问题在于构建".$url." part as there are also speech marks.部分因为还有语音标记。 How would you get around this given that the URL requires the "URL" format.鉴于 URL 需要"URL"格式,您将如何解决这个问题。

Thanks in advance.提前致谢。

You would need to add the proper escape slashes for the quoting issue您需要为引用问题添加正确的转义斜杠

$text = "<a href="".$url.">".$title."</a> <br> \n";

becomes变成

$text = "<a href=\"".$url."\">".$title."</a> <br> \n";

Or you could mix single and double quotes and not use escapes -或者你可以混合单引号和双引号而不使用转义符 -

$text = "<a href='".$url."'>".$title."</a> <br> \n";

只要 $text 字符串是双引号,你就可以这样做:

$text = "<a href='{$url}'>{$title}</a> <br> \n";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM