Project

General

Profile

SnipPets » History » Version 7

Simon, 05/02/2012 01:59 PM

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