简体   繁体   English

如何在PHP中回显正斜杠

[英]How do I echo forward slash in php

I want to echo forward slash in php as follows 我想在PHP中回显正斜杠,如下所示

<a class="submenu" href="<?php echo base_url('products').'/'.rawurlencode('Agarbatte/Candle');?>"

Agarbatte/Candle is not a directory but when i do this, href takes this as directory and give me the error page not found. Agarbatte/Candle不是目录,但是当我执行此操作时, href将此目录作为目录并给我未找到的错误页面。

Any help will be appreciated. 任何帮助将不胜感激。

A slash in the URL will always be treated as a directory separator. URL中的斜杠将始终被视为目录分隔符。 And if you replace it with something else you won't have a slash anymore. 而且,如果将其替换为其他东西,您将不再有任何斜线。 But that's most likely the easiest solution.. 但这很可能是最简单的解决方案。

If you have a 1:1 mapping between the path in the URL and your filesystem you are out of luck. 如果URL路径和文件系统之间有1:1映射,那么您将很不走运。 If your application uses a "routing layer" though, you can modify it to not treat the / as a separator when some criterium is met. 但是,如果您的应用程序使用“路由层”,则可以修改它,使其在满足某些标准时不将/视为分隔符。

Just use 'urlencode' on the 'Agarbatte/Candle'. 只需在“ Agarbatte /蜡烛”上使用“ urlencode”。 I do not see a special need for 'rawurlencode'. 我对“ rawurlencode”没有特别的需求。

Though you can use the urlencode() function as others have said, I recommend not getting into this habit. 尽管您可以像其他人所说的那样使用urlencode()函数,但我还是建议您不要养成这种习惯。 Assuming you have these products in a database, use the id and you'll never run into these issues. 假设您将这些产品存储在数据库中,请使用id并且永远不会遇到这些问题。

For example if the Candle product has id of 6 in the database... the PHP should resolve to: <a class="submenu" href='products/6'> 例如,如果Candle产品在数据库中的id为6,则PHP应该解析为: <a class="submenu" href='products/6'>

Furthermore, there are some shortcuts you can take here which will help you in the long run. 此外,您可以在此处采取一些捷径,从长远来看将为您提供帮助。

  1. Instead of <?php echo "something" ?> turn on php short tags in php.ini and use <?= "something" ?> 代替<?php echo "something" ?>打开php.ini中的php short标签,并使用<?= "something" ?>
  2. You don't need to call base_url every time you write a link. 您无需在每次编写base_url都调用base_url Use the <base> HTML tag appropriately: <base href="http://localhost/yourapp/"> in <head> 适当使用<base> HTML标记: <head> <base href="http://localhost/yourapp/"> <head>

So... with that said after using 1 and 2: 所以...使用1和2之后说

<a href='products/<?= $id;?>'><?= $name_of_product ?></a>

如何在网址中然后在输出页面上使用“ Agarbatte-Candle”

$url_bit=string_replace('-','/',$url_bit);

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

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