【问题标题】:php and echoing out string with html tags. string space truncate string and   also not working as a replacementphp 并用 html 标签回显字符串。字符串空间截断字符串,也不能作为替代品
【发布时间】:2015-08-26 09:49:41
【问题描述】:

MySql:我的产品表设置如下:

pg_id | pg_name

1 |比萨饼
2 |儿童菜单


Php:在遍历 MySQL 表中的记录时回显 html。

<?php do { ?>
<li>
  <?php echo "<a href=". "products.php?p_group=" .$row_getproductnames[ 'pg_name'] . ">"; ?>

  <?php echo $row_getproductnames[ 'pg_name']; ?>
  </a>
</li>

<?php } while ($row_getproductnames=mysql_fetch_assoc($getproductnames)); ?>

我的超链接:products.php 页面的链接对于包含空格的记录应该如下所示。此帖并在产品页面中正确引用产品名称。

http://127.0.0.1/products.php?p_group=Pizza's calzone

但它会在空格后截断为

http://127.0.0.1/products.php?p_group=Pizza's

我已经检查了许多示例,例如使用&amp;nbsp; 代替空格、Html 加密或解密等。仍然无法正确链接字符串。任何帮助将不胜感激。

【问题讨论】:

  • 使用 %20 代替 URL 中的空格
  • var_dump($row_getproductnames[ 'pg_name']); 得到什么?
  • 它回显格式和字符串长度:string(15) "Pizza's calzone"

标签: php html mysql string


【解决方案1】:

你需要用双引号引用href

echo "<a href=\"products.php?p_group=" .$row_getproductnames[ 'pg_name'] . "\">"

如果您使用单引号或不使用引号,则浏览器会误解 pg_name 中的 '

【讨论】:

    【解决方案2】:

    您不使用引号?我不确定这是导致它的原因,但通常有任何解析问题,引号都会解决它。

    尝试替换这一行:

    <?php echo "<a href='products.php?p_group=" .$row_getproductnames[ 'pg_name'] . "'>"; ?>
    

    【讨论】:

    • 感谢您的回复。不幸的是,我之前在引号中引用过它,结果是:products.php?p_group=Pizza。字符串在 's 处断开。
    • 尝试双引号或添加斜杠以阻止单引号破坏模式
    【解决方案3】:

    如果您尝试创建一个有效的 URL,您可能需要将空格替换为 +%20。要么都行。我还建议删除撇号:

    $new_url = str_replace(" ","+", $old_url); //Replace space
    $new_url = str_replace("'","", $new_url ); //Remove apostrophe 
    

    编辑:

    如果您需要使用名称参数从数据库中检索项目,您可以通过“重新替换”另一端的空格和撇号字符来完成,如下所示:

    构建网址:

    $new_url = str_replace(" ","+", $old_url); //Replace space
    $new_url = str_replace("'","APOSTROPHE", $new_url ); //Remove apostrophe 
    

    然后在您将执行SELECT 查询的页面上:

    $product_name = str_replace("+"," ", $product_name); //Put spaces back
    $product_name = str_replace("APOSTROPHE","'", $product_name ); //put apostrophes back 
    

    然而,有更简单的方法可以将值发送到其他页面,例如发送POST 请求

    【讨论】:

    • 感谢您的回复。在我发布到页面之前,我有这个编码。它删除了引号,但不能解决空间问题。我的客户正在设置产品页面中的名称。然后,超链接通过 url 将产品名称引用到产品页面,其中选择根据存储在 items 表中的此名称检索产品。 "选择 * from itemtable where pg_name = ''Pizza's calzone"。
    • 我已经修改了我认为你想要的答案。查看我的编辑
    • 感谢您的回复。看起来是在同一页上reguard this。我正在使用 $_Post 检查解决此问题的方法。 ",str_replace(' ', '_',$row_getproductnames['pg_name'])) 。 "'>"; ?> 会给我这个 URL:products.php?p_group=Pizzas_calzone。将 products.php 页面上的帖子抓取到一个 var 中,然后将字符串操作回正确的格式。谢谢您的意见。赞赏。
    • 完全没问题! :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-11
    • 1970-01-01
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多