Project

General

Profile

SnipPets » History » Version 6

Simon, 01/20/2011 12:07 PM

1 1 Simon
2 6 Simon
h1. Snippets
3
 
4
5
6
h2. 15 usefull regex
7
8 1 Simon
http://www.catswhocode.com/blog/15-php-regular-expressions-for-web-developers
9
10
11 6 Simon
h2. Convert relative to absolute URL
12
13
14
*Certainly the best and safer way*
15 1 Simon
 - http://nadeausoftware.com/node/79
16 4 Simon
17 6 Simon
*the way it simply works*
18 4 Simon
19 6 Simon
20 4 Simon
 - from http://www.howtoforge.com/forums/showthread.php?t=4
21 6 Simon
<pre>
22 4 Simon
function absolute_url($txt, $base_url){
23
  $needles = array('href="', 'src="', 'background="');
24 6 Simon
  $new_txt = _;
25 4 Simon
  if(substr($base_url,-1) != '/') $base_url .= '/';
26
  $new_base_url = $base_url;
27
  $base_url_parts = parse_url($base_url);
28
29
  foreach($needles as $needle){
30
    while($pos = strpos($txt, $needle)){
31
      $pos += strlen($needle);
32
      if(substr($txt,$pos,7) != 'http://' && substr($txt,$pos,8) != 'https://' && substr($txt,$pos,6) != 'ftp://' && substr($txt,$pos,9) != 'mailto://'){
33 1 Simon
        if(substr($txt,$pos,1) == '/') $new_base_url = $base_url_parts['scheme'].'://'.$base_url_parts['host'];
34 4 Simon
        $new_txt .= substr($txt,0,$pos).$new_base_url;
35
      } else {
36 1 Simon
        $new_txt .= substr($txt,0,$pos);
37 4 Simon
      }
38
      $txt = substr($txt,$pos);
39 1 Simon
    }
40
    $txt = $new_txt.$txt;
41 6 Simon
    $new_txt = _;
42 1 Simon
  }
43
  return $txt;
44
}  
45 6 Simon
</pre>
46 1 Simon
47
48
49 6 Simon
h2. Remove trailing whitespaces
50
51
52 1 Simon
http://jimbojw.com/wiki/index.php?title=How_to_find_PHP_files_with_trailing_whitespace
53
54 6 Simon
*Command Line :* 
55
<pre>
56 1 Simon
echo '<?php foreach (glob("**/*.php") as $file){if (preg_match( "/\\?".">\\s\\s+\\Z/m", file_get_contents($file))) echo("$file\n");} ?>' | php
57 6 Simon
</pre>
58 1 Simon
59 2 Simon
60 6 Simon
*PHP file script :*
61
<pre>
62 1 Simon
<?php
63
foreach (glob('**/*.php') as $file){
64
  if (preg_match('/\\?'.'>\\s\\s+\\Z/m',file_get_contents($file)))
65
    echo("$file\n");
66
}
67
?>
68 6 Simon
</pre>
69 1 Simon
70
71
72 6 Simon
h2. Remove .svn folders under Windows
73
74
75
<pre>
76 3 Simon
for /r repository %f in (.svn) do rd /S /Q "%f"
77 6 Simon
</pre>
78 3 Simon
79
from : http://www.jonathan-petitcolas.com/fr/supprimer-recursivement-les-dossiers-svn-sous-windows/
80
81
82 6 Simon
h2. Ignore specific subfolders in .htaccess (URL rewrite)
83
 
84 3 Simon
85 6 Simon
86
*Method 1 :*
87
<pre>
88
  [[RewriteEngine]] on
89 3 Simon
  #
90
  # stuff to let through (ignore)
91 6 Simon
  [[RewriteCond]] %{REQUEST_URI} "/folder1/" [OR]
92
  [[RewriteCond]] %{REQUEST_URI} "/folder2/"
93
  [[RewriteRule]] (.*) $1 [L]
94 3 Simon
  #
95 6 Simon
</pre>
96 3 Simon
97 6 Simon
*Method 2 :*
98
<pre>
99
[[RewriteEngine]] On
100
[[RewriteBase]] /
101 1 Simon
Rewritecond  %{REQUEST_URI} !^/linea21/.*$ [NC]
102 6 Simon
[[RewriteCond]] %{REQUEST_FILENAME} !-f
103
[[RewriteCond]] %{REQUEST_FILENAME} !-d
104
[[RewriteRule]] . /index.php [L]
105
</pre>
106 1 Simon
107 6 Simon
(Ignore rules for _linea21_ sub-folder)