dre93205 2013-07-16 20:25
浏览 65

str_replace不替换从数据库中提取的url

I am trying to extract a filepath of an image from a MySQL database and turn it into a URL. However when I pass it through str_replace no changes are been made. I have set up a test document with a path preset and it works without any problems.

My code that extracts the path from the database is

$user_image = get_web_path($row['user_pic_path']);

where

function get_web_path($file_system_path) {
   return str_replace(var_dump($_SERVER['DOCUMENT_ROOT']), '', var_dump($file_system_path));
}

editted

My test code is:

<?php
   echo "DOCUMENT_ROOT: {$_SERVER['DOCUMENT_ROOT']}";
   $image_sample_path = "C:/wamp/www/website/images/image123.jpg";
   $web_image_path = str_replace($_SERVER['DOCUMENT_ROOT'], '', $image_sample_path);

   echo "<br /><br />CONVERTED PATH: {$web_image_path}";
?>

Which outputs:

   /website/images/image123.jpg"

However when extracting the value from the database, C:/wamp/www from the 'user_pic_path' field isn't being replaced.

var_dump('DOCUMENT_ROOT') gives

string 'C:/wamp/www' (length=11)

var_dump($file_system_path)) gives

string 'C:/wamp/www/website/images/image123.jpg' (length=39)
  • 写回答

1条回答 默认 最新

  • douwenan9849 2013-07-16 20:28
    关注

    Why do you have curly braces around $_SERVER['DOCUMENT_ROOT'] ?

    Try replacing that with:

    str_replace($_SERVER['DOCUMENT_ROOT'], '', $file_system_path);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr
  • ¥15 怀疑手机被监控,请问怎么解决和防止
  • ¥15 Qt下使用tcp获取数据的详细操作
  • ¥15 idea右下角设置编码是灰色的
  • ¥15 全志H618ROM新增分区