neridaj Posted April 21, 2008 Share Posted April 21, 2008 Hello, I'm trying to replace the "%20" (space) from the query string with " " but my page just keeps blowing up when I use str_replace(). I have directories which contain photos of houses and I need the name of the directories to have spaces as they are also used for display purposes. However, when "%20" shows up in the query string it blows up the page and nothing is displayed. I tried running it through str_replace() to replace the "%20" but this doesn't work either. function get_imgarr() { $rpa = $_GET['pa']; $pa = str_replace("%20", " ", $rpa); var_dump($pa); if(!valid_propadd($pa)) die("invalid property address!"); else $propadd = $pa; $userfolder = $_SESSION['valid_user']; $dir = get_imgdir(); $files = @scandir($dir); foreach($files as $value) { // check for image files if(valid_image_file($value)) // build image array $imgarr[] = $value; } return $imgarr; } Any ideas? Thanks, Jason Link to comment https://forums.phpfreaks.com/topic/102226-str_replace-for-20/ Share on other sites More sharing options...
dptr1988 Posted April 21, 2008 Share Posted April 21, 2008 Try using urldecode() to decode your string http://us.php.net/manual/en/function.urldecode.php Link to comment https://forums.phpfreaks.com/topic/102226-str_replace-for-20/#findComment-523328 Share on other sites More sharing options...
AndyB Posted April 21, 2008 Share Posted April 21, 2008 Retrospectively, wouldn't it have been better to have avoided spaces in file names and folder names (using an underscore is a common practice) and then when you want to display a file/folder name use str_replace to change _ to a space? Link to comment https://forums.phpfreaks.com/topic/102226-str_replace-for-20/#findComment-523334 Share on other sites More sharing options...
neridaj Posted April 22, 2008 Author Share Posted April 22, 2008 I decided to do this after no luck with urldecode(), however I get the same results with str_replace(). I don't understand what's going on, the page just stops when the query string contains anything other than alphanumeric. I tried commenting out my regex check and I still get the same thing, when I use only integers it works fine. Here is the code sent to the query string: function display_folders() { //$photoarr = get_imgarr(); $username = $_SESSION['valid_user']; $result = get_agent_info($username); $row = $result->fetch_array(MYSQLI_ASSOC); echo '<a href="logout.php">Logout</a>'; $userfolder = $_SESSION['valid_user'] . '/'; $dir = 'members/' . $userfolder; $files = scandir($dir); $count = 0; foreach($files as $value) { if(!in_array($value, array('.', '..'))) { // check for folders if(is_dir($dir.DIRECTORY_SEPARATOR.$value)) { $count++; } } } echo '<table align="center" border="0" cellpadding="5"><tr><th colspan="'. $count .'">'. $row['agent_name'] .'</th></tr><tr>'; foreach($files as $value) { if(!in_array($value, array('.', '..'))) { // check for folders if(is_dir($dir.DIRECTORY_SEPARATOR.$value)) { $size = getimagesize($dir . $value . '.jpg'); list($width, $height) = $size; $dimarr = array("width" => $width, "height" => $height); $scalewidth = .04 * $dimarr["width"]; $scaleheight = .04 * $dimarr["height"]; $rvalue = str_replace("_", " ", $value); printf('<td><a href="preview.php?pa=%s">'. '<img src="'. $dir . $value .'.jpg" width="'. $scalewidth .'" height="'. $scaleheight .'" />'. '<br />%s<a/></td>', $value, $rvalue); } } } echo '</tr></table>'; } Is it illegal to have underscores in the query string? Thanks for the replies, Jason Link to comment https://forums.phpfreaks.com/topic/102226-str_replace-for-20/#findComment-523699 Share on other sites More sharing options...
neridaj Posted April 22, 2008 Author Share Posted April 22, 2008 Got it, had to modify the regex. Thanks guys. Link to comment https://forums.phpfreaks.com/topic/102226-str_replace-for-20/#findComment-523717 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.