【问题标题】:How to remove white spaces in a path?如何删除路径中的空格?
【发布时间】:2016-06-16 17:48:12
【问题描述】:

我有这个样式代码来显示背景图像。我尝试 trim() 和 urlencode() 删除空格,但没有奏效。

$get_active_quiz = mysql_query("SELECT * FROM questions WHERE status ='active' AND title = '$title'");
$row_active_quiz = mysql_fetch_array($get_active_quiz);
$background_image = $row_active_quiz['cover_image'];

<body style="
background-image: url(<?php echo $background_image ?>);
background-size:100% auto;
background-repeat: no-repeat;">

【问题讨论】:

  • 没有工作是什么意思?,你得到错误了吗?,你得到输出但url是空的?,还是什么?
  • @Neat 没有得到输出
  • 将你的body标签写成:- echo " ";

标签: php css image background-image


【解决方案1】:
$new_background_image = str_replace(' ', '%20', $background_image); 

【讨论】:

    【解决方案2】:

    对于空格,使用 str_replace:

    $string = str_replace(' ', '', $string);
    

    对于所有空格,使用 preg_replace:

    $string = preg_replace('/\s+/', '', $string);
    

    【讨论】:

      【解决方案3】:

      您可以使用 str_replace 替换空格:

      $background_image = str_replace(' ', '', $background_image);
      

      或者对于所有空格,preg_replace:

      $background_image = preg_replace('/\s+/', '', $background_image);
      

      【讨论】:

      • 第二个更好
      • 对于简单的“空格”删除,不需要正则表达式@bub
      【解决方案4】:

      使用 urlencode。这将解决问题。

      【讨论】:

      • 就像作者说的:我试过 trim() 和 urlencode() 去除空格,但是没有用。
      • $new_image = str_replace(' ', '%20', $new_image);使用这个
      猜你喜欢
      • 2021-12-23
      • 1970-01-01
      • 2013-06-29
      • 2010-11-29
      • 2016-08-30
      • 2014-07-19
      • 1970-01-01
      相关资源
      最近更新 更多