Project

General

Profile

SnipPets » History » Version 5

Simon, 01/20/2011 12:07 PM

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