简体   繁体   中英

How do I echo forward slash in php

I want to echo forward slash in php as follows

<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.

Any help will be appreciated.

A slash in the URL will always be treated as a directory separator. 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. 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'. I do not see a special need for 'rawurlencode'.

Though you can use the urlencode() function as others have said, I recommend not getting into this habit. Assuming you have these products in a database, use the id and you'll never run into these issues.

For example if the Candle product has id of 6 in the database... the PHP should resolve to: <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" ?>
  2. You don't need to call base_url every time you write a link. Use the <base> HTML tag appropriately: <base href="http://localhost/yourapp/"> in <head>

So... with that said after using 1 and 2:

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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