Log in

View Full Version : Resolved preg_replace



ggalan
02-24-2012, 11:32 PM
i am trying to replace decorative quotes but i keep getting this http://www.demographx.com/upload/view/Untitled-1.jpg

it doesnt work here


function ListFiles($dir) {

if($dh = opendir($dir)) {

$files = Array();
$inner_files = Array();

while($file = readdir($dh)) {

$file = trim( preg_replace( "/’/", ''', $file ) );
$file = utf8_encode($file);

if($file != "." && $file != ".." && $file[0] != '.') {
if(is_dir($dir . "/" . $file)) {
$inner_files = ListFiles($dir . "/" . $file);
if(is_array($inner_files)) $files = array_merge($files, $inner_files);
} else {
array_push($files, $dir . "/" . $file);//$dir = directory name
//array_push($files, $dir);
}
}

}// while



closedir($dh);
return $files;
}
}

foreach (ListFiles('./') as $key=>$file){
echo $file ."<br />";
}

but it seems to work here


$folder = "./";
$handle = opendir($folder);


while ($file = readdir($handle)) {
$file = trim( preg_replace( "/’/", ''', $file ) );
$file = utf8_encode($file);
$files[] = $file;
}
closedir($handle);


foreach ($files as $file) {
echo "$file"."<br />";
}

any suggestions why it wont work on the first list?

ggalan
02-25-2012, 12:52 AM
this fixed the single curly quote

$str = preg_replace('/[\x91\x92]/u', """, $str);quotes//when i spell out the quote in special html char, it converts it to a quote

james438
02-25-2012, 05:10 AM
Try escaping your literals instead.


<?php
$file="this is ’a’ test.";
echo "$file<br>";
$file = trim(preg_replace( "/\’/", '\'', $file));
echo "$file";
?>

Any non alpha numeric value can and probably should be escaped if you want to declare it as a literal value. This is especially true of delimiters. In the above example the forward slash is used as the delimiter and in the second argument of the regular expression the single quote is used as the delimiter.