Results 1 to 3 of 3

Thread: preg_replace

  1. #1
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default preg_replace

    i am trying to replace decorative quotes but i keep getting this

    it doesnt work here
    Code:
    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
    Code:
    $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?
    Last edited by ggalan; 02-25-2012 at 12:53 AM.

  2. #2
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

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

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,417
    Thanks
    102
    Thanked 116 Times in 114 Posts

    Default

    Try escaping your literals instead.

    PHP Code:
    <?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.
    To choose the lesser of two evils is still to choose evil. My personal site

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •