2016-03-03 120 views
-1

我有这种风格的代码来显示背景图片。我试过修剪()和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;"> 
+1

什么_didn't work_是什么意思?,你得到一个错误吗?,你得到的输出,但url是空的,或什么? – Neat

+0

@Neat没有得到输出 –

+0

将您的身体标记写为: - echo““; –

回答

-1

使用urlencode。这将解决问题。

+0

像作者说:_I尝试trim()和urlencode()删除空格,但没有工作._ – Neat

+0

$ new_image = str_replace('','%20',$ new_image);使用这个 –

+0

@syonzubair谢谢! –

0

可以使用str_replace函数替换空格:

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

或所有的空格符,preg_replace:

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

第二个是更好的 –

+0

对于简单的'空间'删除正则表达式不应该需要@bub – Sudoscience

0

对于刚刚空间,使用str_replace函数:

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

对于所有的空格,使用preg_replace:

$string = preg_replace('/\s+/', '', $string); 
0
$new_background_image = str_replace(' ', '%20', $background_image);