【问题标题】:PHP remove special character '%' from stringPHP从字符串中删除特殊字符'%'
【发布时间】:2015-12-11 16:19:59
【问题描述】:

我想删除 '%'

echo '<a href="/coupon/'.$post->slug.'/'.$dcount.'/'.strtolower(str_replace(' ','-', $drow['title'])).'" target="_blank">';

有什么建议吗?谢谢

【问题讨论】:

  • 你已经知道替换东西的功能了:str_replace(),那么问题出在哪里?
  • 由于某种原因无法正常工作...您能提供示例吗?
  • “不工作”是什么意思?你有任何错误吗?在文件顶部添加error reportingini_set("display_errors", 1); error_reporting(E_ALL);
  • 你想在哪里替换%% 包含什么?你试过urlencode; php.net/manual/en/function.urlencode.php?
  • @JoseSalazar 阅读有关str_replace()的手册

标签: php string replace special-characters


【解决方案1】:
str_replace([' ','%'], ['-','&#37;'], $drow['title']);

str_replace 允许您指定多个替换,如上所述。不确定你想用什么替换%,所以我输入了&amp;#37;,这是% 的HTML 转义符号。

【讨论】:

    【解决方案2】:

    使用preg_replace 代替正则表达式

    preg_replace('/[ %]/','-', $drow['title']))
    

    了解preg_replacehttp://php.net/manual/en/function.preg-replace.php

    了解正则表达式:http://www.regular-expressions.info/

    【讨论】:

    • 感谢您的帮助!
    猜你喜欢
    • 2011-08-29
    • 1970-01-01
    • 2011-04-11
    • 2016-01-23
    • 2019-11-09
    相关资源
    最近更新 更多