<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>piouPiouM&#039;s dev&#187; Archives pour le tag PHP – piouPiouM&#039;s dev</title>
	<atom:link href="http://pioupioum.fr/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://pioupioum.fr</link>
	<description>Bloc-note d&#039;un développeur web</description>
	<lastBuildDate>Thu, 29 Jul 2010 16:47:44 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>TextMate&#160;: commande &#8220;Export words&#8221;</title>
		<link>http://pioupioum.fr/snippets/textmate-commande-exporter-mots-word-wrap.html</link>
		<comments>http://pioupioum.fr/snippets/textmate-commande-exporter-mots-word-wrap.html#comments</comments>
		<pubDate>Sun, 16 May 2010 14:30:47 +0000</pubDate>
		<dc:creator>piouPiouM</dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[licence MIT]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[TextMate]]></category>

		<guid isPermaLink="false">http://pioupioum.fr/?p=380</guid>
		<description><![CDATA[À l&#8217;intérieur du code source d&#8217;une classe PHP, je souhaite stocker dans un tableau la liste des mots de la version populaire de l&#8217;incontournable texte Lorem ipsum. Jusque là rien de bien sorcier. Mais pour des raisons de lisibilité et de conventions de codage je veux obtenir des lignes formées par un maximum de 72 [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start --><p>À l&#8217;intérieur du code source d&#8217;une classe PHP, je souhaite stocker dans un tableau la liste des mots de la version populaire de l&#8217;incontournable texte <em><a href="http://fr.wikipedia.org/wiki/Faux-texte#Lorem_ipsum_.28version_populaire.29" title="Faux-texte - Wikipédia">Lorem ipsum</a></em>. Jusque là rien de bien sorcier. Mais pour des raisons de lisibilité et de conventions de codage je veux obtenir des lignes formées par un maximum de 72 caractères.</p>

<p>Certes je peux <em>m&#8217;amuser</em> à le faire à la main, mais pourquoi me fatiguer quand un langage de script tel que <strong>PHP</strong> me tend la main pour automatiser la procédure&#160;? Et, cerise sur le gâteau, si en plus je peux l&#8217;intégrer à mon éditeur préféré, <a href="http://pioupioum.fr/tag/textmate/" title="Archives pour le tag TextMate – piouPiouM&#039;s dev">TextMate</a>.</p>

<p><div id="attachment_386" class="wp-caption aligncenter" style="width: 458px"><a href="http://assets1.pioupioum.fr/uploads/2010/05/export-words.png"><img src="http://assets1.pioupioum.fr/uploads/2010/05/export-words-448x350.png" alt="Exemple d&#039;action de la commande TextMate Export words" title="TextMate Export words" width="448" height="350" class="size-medium wp-image-386" /></a><p class="wp-caption-text">Exemple d&#8217;action de la commande TextMate Export words</p></div><span id="more-380"></span></p>

<h2>Intégration dans Textmate</h2>

<ol>
<li>Allez dans le menu <span lang="en">Bundles</span> > <span lang="en">Bundle Editor</span> > <span lang="en">Show Bundle Editor</span> puis créez une nouvelle commande à l&#8217;aide de l&#8217;action <span lang="en">New Command</span>.</li>
<li>Donnez à la commande le nom <span lang="en">Export words</span>.</li>
<li>Veillez à ce que <span lang="en">Save</span> soit défini à <span lang="en">Nothing</span>.</li>
<li>Sélectionnez pour <span lang="en">Input</span> les options <span lang="en">Selected Text or Nothing</span>.</li>
<li>Placez <span lang="en">Output</span> à <span lang="en">Replace Selected Text</span>.</li>
<li>Choisissez une <span lang="en">Activation</span> de type <span lang="en">Key Equivalent</span> et définnez un raccourci. J&#8217;ai opté pour la séquence <kbd>⌥⇧W</kbd>.</li>
<li>Laissez vide le champ <span lang="en">Scope Selector</span> afin de pouvoir jouir de la commande depuis n&#8217;importe quel type de fichier et scope.</li>
<li>Enfin, copiez-collez le code ci-dessous dans le champ <span lang="en">Command(s)</span>.</li>
</ol>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">#!/usr/bin/env php -n
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * Export a wrapped representation of the words of a text.
 *
 * Optional parameters in a line that follows the text:
 *      l&lt;size&gt;. The column width. Default: 72.
 *      s&lt;characters&gt;. Characters to remove. Default: punctuation.
 *      o&lt;asc|desc&gt;. Sort order. Default: none.
 */</span>
<span style="color: #666666; font-style: italic;">/*
# ***** BEGIN LICENSE BLOCK *****
#
# Copyright (C) 2010 Mehdi Kabab &lt;http://pioupioum.fr/&gt;
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the &quot;Software&quot;), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# ***** END LICENSE BLOCK ***** */</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">version_compare</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">PHP_VERSION</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'5.0.0'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'&lt;'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'You must have PHP &gt;= 5.0.0!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$lenght</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">72</span><span style="color: #339933;">;</span>      <span style="color: #666666; font-style: italic;">// the default column width.</span>
<span style="color: #000088;">$strip</span>  <span style="color: #339933;">=</span> <span style="color: #0000ff;">'\p{P}'</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// removes the ponctuation</span>
<span style="color: #000088;">$sort</span>   <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>   <span style="color: #666666; font-style: italic;">// no sorting</span>
&nbsp;
<span style="color: #000088;">$raw</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'php://filter/read=string.tolower/resource=php://stdin'</span><span style="color: #339933;">,</span>
            FILE_IGNORE_NEW_LINES <span style="color: #339933;">|</span> FILE_SKIP_EMPTY_LINES<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$raw</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_pop</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$raw</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#^(?:\s{0,}(?:l(?&lt;lenght&gt;\d+)|s(?&lt;strip&gt;.*)|o(?&lt;sort&gt;asc|desc))\s{0,})+$#u'</span><span style="color: #339933;">,</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'lenght'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'lenght'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000088;">$lenght</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'lenght'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'strip'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'strip'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000088;">$strip</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">'|'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'strip'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'sort'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'sort'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000088;">$sort</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'sort'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">array_push</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$raw</span><span style="color: #339933;">,</span> <span style="color: #000088;">$args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$raw</span> <span style="color: #339933;">=</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$raw</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Removes punctuation and user characters</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$strip</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$strip</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'\#'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$strip</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$raw</span>   <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#('</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$strip</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">')#u'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #000088;">$raw</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Estimating indent</span>
<span style="color: #000088;">$start_index</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'TM_INPUT_START_LINE_INDEX'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$start_index</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'TM_INPUT_START_LINE_INDEX'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'YES'</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'TM_SOFT_TABS'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$tab</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_repeat</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'TM_TAB_SIZE'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$tab</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$tab_count</span>    <span style="color: #339933;">=</span> <span style="color: #990000;">floor</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$start_index</span> <span style="color: #339933;">/</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'TM_TAB_SIZE'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$extra_spaces</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$start_index</span> <span style="color: #339933;">%</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'TM_TAB_SIZE'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$tabs</span>         <span style="color: #339933;">=</span> <span style="color: #990000;">str_repeat</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$tab</span><span style="color: #339933;">,</span> <span style="color: #000088;">$tab_count</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #990000;">str_repeat</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$extra_spaces</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$tabs_len</span>     <span style="color: #339933;">=</span> <span style="color: #000088;">$tab_count</span> <span style="color: #339933;">*</span> <span style="color: #000088;">$_SERVER</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'TM_TAB_SIZE'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$extra_spaces</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Extracting words now!</span>
<span style="color: #000088;">$words</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#\s+#'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$raw</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$words</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_map</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'trim'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$words</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$words</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_filter</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_unique</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$words</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// removes empty fields</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$sort</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">natsort</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$words</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'desc'</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$sort</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$words</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_reverse</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$words</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$words</span> <span style="color: #339933;">=</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'%s\'%s\''</span><span style="color: #339933;">,</span>
                 <span style="color: #000088;">$tabs</span><span style="color: #339933;">,</span>
                 <span style="color: #990000;">wordwrap</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;', '&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$words</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$lenght</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$tabs_len</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #006699; font-weight: bold;">$tabs</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span> <span style="color: #339933;">!==</span> <span style="color: #000088;">$start_index</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$words</span> <span style="color: #339933;">=</span> <span style="color: #990000;">ltrim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$words</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;''&quot;</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$words</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$words</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<h2>Utilisation de la commande TextMate <strong>Export words</strong></h2>

<p>Sélectionnez le texte à transformer. La transformation du texte se contrôle en ajoutant à la suite une ligne d&#8217;arguments <strong>optionnels</strong>&#160;:</p>

<ul>
<li><strong><code>l&lt;size&gt;</code></strong>. Ajuster la longueur de ligne. Exemple&#160;: <code>l80</code> pour effectuer une coupure à la colonne 80. Défaut&#160;: 72.</li>
<li><strong><code>s&lt;characters&gt;</code></strong>. Exemple&#160;: <code>slorem|ipsum</code> pour exclure les mots <em>lorem</em> et <em>ipsum</em>. Défaut&#160;: toute ponctuation.</li>
<li><strong><code>o&lt;asc|desc&gt;</code></strong>. Trier la sortie selon les ordres <code>asc</code>endant et <code>desc</code>endant. Défaut&#160;: aucun tri n&#8217;est réalisé.</li>
</ul>

<p>Notez que si votre sélection est précédée par une indentation, alors le script va appliquer cette dernière à chacune des lignes générées en se conformant aux options courantes de TextMate&#160;: il générera des espaces si le mode <span lang="en">Soft Tabs</span> est activé. Sinon des tabulations complétées d&#8217;éventuels espaces.</p>

<h3 class='related_post_title'>Continuez votre lecture sur des sujets similaires</h3>

<ul class='related_post'><li><a href='http://pioupioum.fr/outils-astuces/license-helper-textmate-bundle.html' title='Bundle License Helper pour TextMate'>Bundle License Helper pour TextMate</a></li><li><a href='http://pioupioum.fr/outils-astuces/textmate-convertir-couleur-hexadecimale-html-rgba.html' title='TextMate : convertir une couleur hexadécimale en notation RGBA'>TextMate&#160;: convertir une couleur hexadécimale en notation RGBA</a></li><li><a href='http://pioupioum.fr/snippets/php-convertir-datetime-unix-timestamp.html' title='PHP : convertir un DATETIME en un timestamp UNIX'>PHP&#160;: convertir un DATETIME en un timestamp UNIX</a></li><li><a href='http://pioupioum.fr/snippets/php-uncamel-fonction-convertir-camel-case.html' title='PHP : fonction uncamel() pour inverser une notation en CamelCase'>PHP&#160;: fonction uncamel() pour inverser une notation en CamelCase</a></li><li><a href='http://pioupioum.fr/developpement/javascript-array-intersection.html' title='JavaScript : optimiser le calcul de l&#8217;intersection de tableaux de grandes tailles'>JavaScript&#160;: optimiser le calcul de l&#8217;intersection de tableaux de grandes tailles</a></li></ul>
<!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://pioupioum.fr/snippets/textmate-commande-exporter-mots-word-wrap.html/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP&#160;: installer APC sous Mac OS X Leopard</title>
		<link>http://pioupioum.fr/snippets/php-installer-apc-macosx.html</link>
		<comments>http://pioupioum.fr/snippets/php-installer-apc-macosx.html#comments</comments>
		<pubDate>Thu, 06 May 2010 18:47:30 +0000</pubDate>
		<dc:creator>piouPiouM</dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[APC]]></category>
		<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[sysadmin]]></category>

		<guid isPermaLink="false">http://pioupioum.fr/?p=376</guid>
		<description><![CDATA[Sous Mac OS X Leopard, l&#8217;installation du système de cache APC via PECL est en carton. Pour faire simple, l&#8217;extension n&#8217;est disponible que pour la version CLI de PHP.

Il est cependant possible d&#8217;y remédier1 et ainsi disposer d&#8217;APC avec la configuration de base fourni par Apple.

Récupérer la dernière version d&#8217;APC

$ curl http://pecl.php.net/get/APC -o ~/Downloads/APC-latest.tgz
$ tar [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start --><p>Sous Mac OS X Leopard, l&#8217;installation du système de cache <a href="http://php.net/manual/fr/book.apc.php" title="PHP: APC - Manual">APC</a> <em>via</em> <a href="http://pecl.php.net/" title="PECL :: The PHP Extension Community Library">PECL</a> est en carton. Pour faire simple, l&#8217;extension n&#8217;est disponible que pour la version CLI de PHP.</p>

<p>Il est cependant possible d&#8217;y remédier<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup> et ainsi disposer d&#8217;<strong>APC</strong> avec la configuration de base fourni par Apple.</p>

<h2>Récupérer la dernière version d&#8217;APC</h2>

<pre>$ curl http://pecl.php.net/get/APC -o ~/Downloads/APC-latest.tgz
$ tar -zxf !$ -C ~/Downloads/
$ cd ~/Downloads/APC-3.1.3p1/</pre>

<p>La version utilisée ici est la 3.1.3p1.</p>

<h2>Préparer l&#8217;environnement de compilation</h2>

<pre>$ /usr/bin/phpize
Configuring for:
PHP Api Version:         20041225
Zend Module Api No:      20060613
Zend Extension Api No:   220060519</pre>

<p><span id="more-376"></span></p>

<h2>Lancer la compilation</h2>

<p>Copier-coller dans la commande qui suit dans son intégralité. Attention à ne pas inclure de retours à la ligne.</p>

<pre>MACOSX_DEPLOYMENT_TARGET=10.5 CFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe -no-cpp-precomp" CCFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe" CXXFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -g -Os -pipe" LDFLAGS="-arch ppc -arch ppc64 -arch i386 -arch x86_64 -bind_at_load" ./configure</pre>

<p>Puis un coup de <code>make</code>&#160;:</p>

<pre>$ make
...
----------------------------------------------------------------------
Libraries have been installed in:
   /Users/mehdi/Downloads/APC-3.1.3p1/modules

If you ever happen to want to link against installed libraries
in a given directory, LIBDIR, you must either use libtool, and
specify the full pathname of the library, or use the `-LLIBDIR'
flag during linking and do at least one of the following:
   - add LIBDIR to the `DYLD_LIBRARY_PATH' environment variable
     during execution

See any operating system documentation about shared libraries for
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

Build complete.
Don't forget to run 'make test'.</pre>

<p>Et pour finir, on installe l&#8217;extension APC.</p>

<pre>$ sudo make install
Installing shared extensions:     /usr/lib/php/extensions/no-debug-non-zts-20060613/</pre>

<h2>Activer l&#8217;extension APC pour PHP</h2>

<p>Éditer le fichier <code>php.ini</code> pour ajouter la configuration qui suit&#160;:</p>


<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #000099;">extension</span> <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> apc.so</span>
apc.enabled <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> 1</span>
apc.enable_cli <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> 1</span>
apc.mmap_file_mask <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> /tmp/apc.XXXXXX</span>
apc.num_files_hint <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> 1024</span>
apc.shm_segments <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> 1</span>
apc.shm_size <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> 128</span>
apc.ttl <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> 7200</span>
apc.user_ttl <span style="color: #000066; font-weight:bold;">=</span><span style="color: #660066;"> 7200</span></pre></div></div>


<p>Relancer Apache pour qu&#8217;il prenne en compte la nouvelle extension.</p>

<h3 class='related_post_title'>Continuez votre lecture sur des sujets similaires</h3>

<ul class='related_post'><li><a href='http://pioupioum.fr/snippets/textmate-commande-exporter-mots-word-wrap.html' title='TextMate : commande &laquo;&nbsp;Export words&nbsp;&raquo;'>TextMate&#160;: commande &laquo;&nbsp;Export words&nbsp;&raquo;</a></li><li><a href='http://pioupioum.fr/snippets/php-convertir-datetime-unix-timestamp.html' title='PHP : convertir un DATETIME en un timestamp UNIX'>PHP&#160;: convertir un DATETIME en un timestamp UNIX</a></li><li><a href='http://pioupioum.fr/snippets/php-uncamel-fonction-convertir-camel-case.html' title='PHP : fonction uncamel() pour inverser une notation en CamelCase'>PHP&#160;: fonction uncamel() pour inverser une notation en CamelCase</a></li><li><a href='http://pioupioum.fr/snippets/apache-rotation-logs.html' title='Rotation des logs Apache'>Rotation des logs Apache</a></li><li><a href='http://pioupioum.fr/snippets/convertir-icone-icns-png.html' title='Convertir une icône ICNS en une image PNG'>Convertir une icône ICNS en une image PNG</a></li></ul>

<div class="footnotes">
<hr />
<ol>

<li id="fn:1">
<p>Source&#160;: <a href="http://discussions.apple.com/thread.jspa?threadID=1578979" title="Apple - Support - Discussions - Entropy php5 with GD support and APC ...">Forum du support Apple</a>.&#160;<a href="#fnref:1" rev="footnote">&#8617;</a></p>
</li>

</ol>
</div>
<!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://pioupioum.fr/snippets/php-installer-apc-macosx.html/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PHP&#160;: convertir un DATETIME en un timestamp UNIX</title>
		<link>http://pioupioum.fr/snippets/php-convertir-datetime-unix-timestamp.html</link>
		<comments>http://pioupioum.fr/snippets/php-convertir-datetime-unix-timestamp.html#comments</comments>
		<pubDate>Tue, 09 Mar 2010 23:28:01 +0000</pubDate>
		<dc:creator>piouPiouM</dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[convertir]]></category>
		<category><![CDATA[licence MIT]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://pioupioum.fr/?p=346</guid>
		<description><![CDATA[Mise à jour à 10:57
Plus simple, rapide, intégré, toussa&#160;: utiliser strtotime tout simplement.
Merci à Éric Daspet et à Oncle Tom pour le rappel.

Une fonction utilitaire1 pour convertir une chaîne de type DATETIME ou DATE en un timestamp UNIX.
Les formats pris en charges en entrée sont AAAA-MM-JJ HH:MM:SS et AA-MM-JJ HH:MM:SS. L&#8217;heure étant optionnelle dans le [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start --><p><strong>Mise à jour à 10:57</strong><br />
Plus simple, rapide, intégré, toussa&#160;: utiliser <a href="http://php.net/manual/fr/function.strtotime.php">strtotime</a> tout simplement.<br />
Merci à <a href="http://twitter.com/edasfr/status/10264669320" title="réaction sur twitter">Éric Daspet</a> et à <a href="http://pioupioum.fr/snippets/php-convertir-datetime-unix-timestamp.html#comment-1187" title="réaction dans les commentaires">Oncle Tom</a> pour le rappel.</p>

<p>Une fonction utilitaire<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup> pour convertir une chaîne de type <strong>DATETIME</strong> ou <strong>DATE</strong> en un <strong>timestamp UNIX</strong>.<br />
Les formats pris en charges en entrée sont <code>AAAA-MM-JJ HH:MM:SS</code> et <code>AA-MM-JJ HH:MM:SS</code>. L&#8217;heure étant optionnelle dans le cas de la conversion d&#8217;une date.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * Get UNIX timestamp for a date or date time.
 *
 * Example:
 * &lt;code&gt;
 * &lt;?php
 * unix_timestamp('2010-03-09 22:43:46'); # 1268171026
 * unix_timestamp('10-03-09 22:43:46');   # 1268171026
 * unix_timestamp('2010-03-09');          # 1268089200
 * ?&gt;
 * &lt;/code&gt;
 *
 * @author    Mehdi Kabab &lt;http://pioupioum.fr/&gt;
 * @copyright 2010 Mehdi Kabab
 * @license   http://www.opensource.org/licenses/mit-license.html MIT License
 * @link      http://pioupioum.fr/snippets/php-convertir-datetime-unix-timestamp.html
 *
 * @param string The date or datetime to convert.
 * @return int The UNIX timestamp corresponding to the datetime.
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> unix_timestamp<span style="color: #009900;">&#40;</span><span style="color: #000088;">$date</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$date</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">':'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'-'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$date</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$c</span>    <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'-'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$date</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$c</span>    <span style="color: #339933;">=</span> <span style="color: #990000;">array_pad</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">array_walk</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'intval'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #990000;">mktime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$c</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$c</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$c</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$c</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$c</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$c</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<h3 class='related_post_title'>Continuez votre lecture sur des sujets similaires</h3>

<ul class='related_post'><li><a href='http://pioupioum.fr/snippets/php-uncamel-fonction-convertir-camel-case.html' title='PHP : fonction uncamel() pour inverser une notation en CamelCase'>PHP&#160;: fonction uncamel() pour inverser une notation en CamelCase</a></li><li><a href='http://pioupioum.fr/snippets/textmate-commande-exporter-mots-word-wrap.html' title='TextMate : commande &laquo;&nbsp;Export words&nbsp;&raquo;'>TextMate&#160;: commande &laquo;&nbsp;Export words&nbsp;&raquo;</a></li><li><a href='http://pioupioum.fr/outils-astuces/textmate-convertir-couleur-hexadecimale-html-rgba.html' title='TextMate : convertir une couleur hexadécimale en notation RGBA'>TextMate&#160;: convertir une couleur hexadécimale en notation RGBA</a></li><li><a href='http://pioupioum.fr/outils-astuces/license-helper-textmate-bundle.html' title='Bundle License Helper pour TextMate'>Bundle License Helper pour TextMate</a></li><li><a href='http://pioupioum.fr/developpement/javascript-array-intersection.html' title='JavaScript : optimiser le calcul de l&#8217;intersection de tableaux de grandes tailles'>JavaScript&#160;: optimiser le calcul de l&#8217;intersection de tableaux de grandes tailles</a></li></ul>

<div class="footnotes">
<hr />
<ol>

<li id="fn:1">
<p>vous allez dire qu&#8217;il en existe pléthore, mais je les trouve toutes trop lourdes (conditions, strlen, multiples explode, etc) pour le peu à faire. Sans parler du fait qu&#8217;elles ne supportent uniquement que le format DATETIME.&#160;<a href="#fnref:1" rev="footnote">&#8617;</a></p>
</li>

</ol>
</div>
<!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://pioupioum.fr/snippets/php-convertir-datetime-unix-timestamp.html/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>PHP&#160;: fonction uncamel() pour inverser une notation en CamelCase</title>
		<link>http://pioupioum.fr/snippets/php-uncamel-fonction-convertir-camel-case.html</link>
		<comments>http://pioupioum.fr/snippets/php-uncamel-fonction-convertir-camel-case.html#comments</comments>
		<pubDate>Sun, 28 Feb 2010 18:17:12 +0000</pubDate>
		<dc:creator>piouPiouM</dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[convertir]]></category>
		<category><![CDATA[gist]]></category>
		<category><![CDATA[licence MIT]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://pioupioum.fr/?p=336</guid>
		<description><![CDATA[Une fonction PHP utilitaire pour convertir une chaîne en notation CamelCase en mots réunis par un séparateur (par défaut, le caractère underscore _).

La fonction uncamel() supporte indifféremment, en entrée, les notations lowerCamelCase et UpperCamelCase.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
&#60;?php
/**
 * Reverse a CamelCase string.
 *
 * Examples:
 *     uncamel('lowerCamelCase') === 'lower_camel_case'
 *     [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start --><p>Une fonction <strong><a href="http://fr.wikipedia.org/wiki/PHP" title="PHP: Hypertext Preprocessor - Wikipédia">PHP</a></strong> utilitaire pour convertir une chaîne en notation <strong><a href="http://fr.wikipedia.org/wiki/CamelCase" title="CamelCase - Wikipédia">CamelCase</a></strong> en mots réunis par un séparateur (par défaut, le caractère <em>underscore</em> <code>_</code>).</p>

<p>La fonction <strong><code>uncamel()</code></strong> supporte indifféremment, en entrée, les notations <strong>lowerCamelCase</strong> et <strong>UpperCamelCase</strong>.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * Reverse a CamelCase string.
 *
 * Examples:
 *     uncamel('lowerCamelCase') === 'lower_camel_case'
 *     uncamel('UpperCamelCase') === 'upper_camel_case'
 *     uncamel('ThisIsAString') === 'this_is_a_string'
 *     uncamel('notcamelcase') === 'notcamelcase'
 *     uncamel('lowerCamelCase', ' | ') === 'lower | camel | case'
 *
 * @author    Mehdi Kabab &lt;http://pioupioum.fr/&gt;
 * @copyright 2010 Mehdi Kabab
 * @license   http://www.opensource.org/licenses/mit-license.html MIT License
 * @link      http://pioupioum.fr/snippets/php-uncamel-fonction-convertir-camel-case.html
 *
 * @param  string $content The CamelCase string.
 * @param  string $separator The glue for the compound words. Defaults to '_'.
 * @return string
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> uncamel<span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #339933;">,</span> <span style="color: #000088;">$separator</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'_'</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#(?&lt;=[a-zA-Z])([A-Z])(?=[a-zA-Z])#e'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;'<span style="color: #006699; font-weight: bold;">$separator</span>' . strtolower('<span style="color: #006699; font-weight: bold;">$1</span>')&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$content</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">return</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>


<p>Le <a href="http://gist.github.com/317702" title="gist: 317702 -  GitHub">forker (gist)</a>.</p>

<h3 class='related_post_title'>Continuez votre lecture sur des sujets similaires</h3>

<ul class='related_post'><li><a href='http://pioupioum.fr/snippets/php-convertir-datetime-unix-timestamp.html' title='PHP : convertir un DATETIME en un timestamp UNIX'>PHP&#160;: convertir un DATETIME en un timestamp UNIX</a></li><li><a href='http://pioupioum.fr/snippets/textmate-commande-exporter-mots-word-wrap.html' title='TextMate : commande &laquo;&nbsp;Export words&nbsp;&raquo;'>TextMate&#160;: commande &laquo;&nbsp;Export words&nbsp;&raquo;</a></li><li><a href='http://pioupioum.fr/outils-astuces/textmate-convertir-couleur-hexadecimale-html-rgba.html' title='TextMate : convertir une couleur hexadécimale en notation RGBA'>TextMate&#160;: convertir une couleur hexadécimale en notation RGBA</a></li><li><a href='http://pioupioum.fr/outils-astuces/license-helper-textmate-bundle.html' title='Bundle License Helper pour TextMate'>Bundle License Helper pour TextMate</a></li><li><a href='http://pioupioum.fr/developpement/javascript-array-intersection.html' title='JavaScript : optimiser le calcul de l&#8217;intersection de tableaux de grandes tailles'>JavaScript&#160;: optimiser le calcul de l&#8217;intersection de tableaux de grandes tailles</a></li></ul>
<!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://pioupioum.fr/snippets/php-uncamel-fonction-convertir-camel-case.html/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>PHP&#160;: valider qu&#8217;une URL pointe sur un domaine donné</title>
		<link>http://pioupioum.fr/snippets/php-valider-url-domaine.html</link>
		<comments>http://pioupioum.fr/snippets/php-valider-url-domaine.html#comments</comments>
		<pubDate>Sat, 05 Sep 2009 23:35:41 +0000</pubDate>
		<dc:creator>piouPiouM</dc:creator>
				<category><![CDATA[Snippets]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[URL]]></category>
		<category><![CDATA[validation]]></category>

		<guid isPermaLink="false">http://pioupioum.fr/?p=236</guid>
		<description><![CDATA[Voici une petite fonction PHP pour vérifier qu&#8217;une URL pointe bien sur un domaine donné.

On valide dans un premier temps, via filter_var1, le format de l&#8217;URL. Cette dernière doit notamment être accompagné d&#8217;un protocole. La récupération de l&#8217;hôte depuis l&#8217;URL se fait très simplement avec la fonction parse_url. Enfin, on s&#8217;assure que le domaine cible [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start --><p>Voici une petite fonction PHP pour vérifier qu&#8217;une <abbr title="Uniform Resource Locator">URL</abbr> pointe bien sur un domaine donné.</p>

<p>On valide dans un premier temps, <em>via</em> <a href="http://www.php.net/manual/fr/function.filter-var.php" title="PHP: filter_var - Manual">filter_var</a><sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup>, le format de l&#8217;<abbr title="Uniform Resource Locator">URL</abbr>. Cette dernière doit notamment être accompagné d&#8217;un protocole. La récupération de l&#8217;hôte depuis l&#8217;<abbr title="Uniform Resource Locator">URL</abbr> se fait très simplement avec la fonction <a href="http://www.php.net/manual/fr/function.parse-url.php" title="PHP: parse_url - Manual">parse_url</a>. Enfin, on s&#8217;assure que le domaine cible est bien le domaine principal de l&#8217;hôte.</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Checks a URL belongs to a given domain.
 *
 * @param string $url The URL to test.
 * @param string $domain The target domain.
 * @return boolean
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> isDomainOf<span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span> <span style="color: #000088;">$domain</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">===</span> <span style="color: #990000;">filter_var</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span> FILTER_VALIDATE_URL<span style="color: #339933;">,</span> FILTER_FLAG_HOST_REQUIRED<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$host</span> <span style="color: #339933;">=</span> <span style="color: #990000;">parse_url</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #339933;">,</span> PHP_URL_HOST<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$host</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>boolean<span style="color: #009900;">&#41;</span> <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'#^(?:[^\.]*\.){0,}'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$domain</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'$#'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$host</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p><span id="more-236"></span></p>

<h2>Suite de tests</h2>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$test</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'domain'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'url'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'http://pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'synopsis'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Le domaine testé lui même.'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'domain'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'url'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'http://example.com'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'synopsis'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Un domaine différent.'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'domain'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'url'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'http://www.pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'synopsis'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Un sous-domaine.'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'domain'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'url'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'http://foo.bar.pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'synopsis'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Un double sous-domaine.'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'domain'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'bar.pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'url'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'http://foo.bar.pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'synopsis'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Le dommaine cible contient un sous-domaine.'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'domain'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'url'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'http://assets1.pioupioum.fr/foo/bar.png'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'synopsis'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Un contenu hébergé sur un sous-domaine.'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'domain'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'url'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'http://www.example.com/foo/bar.png'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'synopsis'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Un contenu hébergé sur un domaine différent.'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'domain'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'url'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'http://fakepioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'synopsis'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Un domaine proche.'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'domain'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'url'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'http://www.fakepioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'synopsis'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Un domaine proche avec un sous-domaine.'</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'domain'</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'url'</span>      <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'pioupioum.fr'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'synopsis'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Aucun protocol n\'étant spécifié, la fonction filter_var invalide le test.'</span>
    <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$test</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #000088;">$c</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">;</span> <span style="color: #339933;">++</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$ok</span> <span style="color: #339933;">=</span> isDomainOf<span style="color: #009900;">&#40;</span><span style="color: #000088;">$test</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$test</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'domain'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%s</span><span style="color: #000099; font-weight: bold;">\n</span>[<span style="color: #009933; font-weight: bold;">%s</span>] L'URL <span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #009933; font-weight: bold;">%s</span><span style="color: #000099; font-weight: bold;">\&quot;</span> <span style="color: #009933; font-weight: bold;">%s</span> sur le domaine <span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #009933; font-weight: bold;">%s</span><span style="color: #000099; font-weight: bold;">\&quot;</span>.<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>
        <span style="color: #000088;">$test</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'synopsis'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ok</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'SUCCESS'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'FAIL'</span><span style="color: #339933;">,</span>
        <span style="color: #000088;">$test</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'url'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#40;</span><span style="color: #000088;">$ok</span><span style="color: #009900;">&#41;</span> ? <span style="color: #0000ff;">'pointe bien'</span> <span style="color: #339933;">:</span> <span style="color: #0000ff;">'ne pointe pas'</span><span style="color: #339933;">,</span>
        <span style="color: #000088;">$test</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'domain'</span><span style="color: #009900;">&#93;</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>Qui nous donne l&#8217;affichage suivant&#160;:</p>


<div class="wp_syntax"><div class="code"><pre class="plain" style="font-family:monospace;">Le domaine testé lui même.
[SUCCESS] L'URL &quot;http://pioupioum.fr&quot; pointe bien sur le domaine &quot;pioupioum.fr&quot;.
&nbsp;
Un domaine différent.
[FAIL] L'URL &quot;http://example.com&quot; ne pointe pas sur le domaine &quot;pioupioum.fr&quot;.
&nbsp;
Un sous-domaine.
[SUCCESS] L'URL &quot;http://www.pioupioum.fr&quot; pointe bien sur le domaine &quot;pioupioum.fr&quot;.
&nbsp;
Un double sous-domaine.
[SUCCESS] L'URL &quot;http://foo.bar.pioupioum.fr&quot; pointe bien sur le domaine &quot;pioupioum.fr&quot;.
&nbsp;
Le dommaine cible contient un sous-domaine.
[SUCCESS] L'URL &quot;http://foo.bar.pioupioum.fr&quot; pointe bien sur le domaine &quot;bar.pioupioum.fr&quot;.
&nbsp;
Un contenu hébergé sur un sous-domaine.
[SUCCESS] L'URL &quot;http://assets1.pioupioum.fr/foo/bar.png&quot; pointe bien sur le domaine &quot;pioupioum.fr&quot;.
&nbsp;
Un contenu hébergé sur un domaine différent.
[FAIL] L'URL &quot;http://www.example.com/foo/bar.png&quot; ne pointe pas sur le domaine &quot;pioupioum.fr&quot;.
&nbsp;
Un domaine proche.
[FAIL] L'URL &quot;http://fakepioupioum.fr&quot; ne pointe pas sur le domaine &quot;pioupioum.fr&quot;.
&nbsp;
Un domaine proche avec un sous-domaine.
[FAIL] L'URL &quot;http://www.fakepioupioum.fr&quot; ne pointe pas sur le domaine &quot;pioupioum.fr&quot;.
&nbsp;
Aucun protocol n'étant spécifié, la fonction filter_var invalide le test.
[FAIL] L'URL &quot;pioupioum.fr&quot; ne pointe pas sur le domaine &quot;pioupioum.fr&quot;.</pre></div></div>


<h3 class='related_post_title'>Continuez votre lecture sur des sujets similaires</h3>

<ul class='related_post'><li><a href='http://pioupioum.fr/developpement/jslint-console-rhino.html' title='Une console JSLint pour aider vos validations JavaScript'>Une console JSLint pour aider vos validations JavaScript</a></li><li><a href='http://pioupioum.fr/snippets/textmate-commande-exporter-mots-word-wrap.html' title='TextMate : commande &laquo;&nbsp;Export words&nbsp;&raquo;'>TextMate&#160;: commande &laquo;&nbsp;Export words&nbsp;&raquo;</a></li><li><a href='http://pioupioum.fr/snippets/php-installer-apc-macosx.html' title='PHP : installer APC sous Mac OS X Leopard'>PHP&#160;: installer APC sous Mac OS X Leopard</a></li><li><a href='http://pioupioum.fr/snippets/php-convertir-datetime-unix-timestamp.html' title='PHP : convertir un DATETIME en un timestamp UNIX'>PHP&#160;: convertir un DATETIME en un timestamp UNIX</a></li><li><a href='http://pioupioum.fr/snippets/php-uncamel-fonction-convertir-camel-case.html' title='PHP : fonction uncamel() pour inverser une notation en CamelCase'>PHP&#160;: fonction uncamel() pour inverser une notation en CamelCase</a></li></ul>

<div class="footnotes">
<hr />
<ol>

<li id="fn:1">
<p>la fonction requiers au minimum PHP 5.2.0.&#160;<a href="#fnref:1" rev="footnote">&#8617;</a></p>
</li>

</ol>
</div>
<!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://pioupioum.fr/snippets/php-valider-url-domaine.html/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Phing Header&#160;: une tâche pour ajouter un en-tête aux fichiers</title>
		<link>http://pioupioum.fr/developpement/phing-header-task.html</link>
		<comments>http://pioupioum.fr/developpement/phing-header-task.html#comments</comments>
		<pubDate>Sun, 30 Aug 2009 19:54:24 +0000</pubDate>
		<dc:creator>piouPiouM</dc:creator>
				<category><![CDATA[Développement Web]]></category>
		<category><![CDATA[automatisation]]></category>
		<category><![CDATA[build]]></category>
		<category><![CDATA[phing]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[projet]]></category>

		<guid isPermaLink="false">http://pioupioum.fr/?p=215</guid>
		<description><![CDATA[Une des tâches les plus rébarbatives est bien l&#8217;ajout d&#8217;une licence d&#8217;utilisation à tous les fichiers sources de ses projets. Ajoutez à cela que, comme tout bon développeur, je n&#8217;aime pas répéter la même opération plus de deux fois, je me suis codé l&#8217;an dernier une tâche Phing pour automatiser cela1&#160;: Header.

Présentation

Phing Header se découpe [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start --><p>Une des tâches les plus rébarbatives est bien l&#8217;ajout d&#8217;une licence d&#8217;utilisation à tous les fichiers sources de ses projets. Ajoutez à cela que, comme tout bon développeur, je n&#8217;aime pas répéter la même opération plus de deux fois, je me suis codé l&#8217;an dernier une tâche <a href="http://phing.info/" title="Phing"><strong>Phing</strong></a> pour <strong>automatiser</strong> cela<sup id="fnref:1"><a href="#fn:1" rel="footnote">1</a></sup>&#160;: <em>Header</em>.</p>

<h2>Présentation</h2>

<p><strong>Phing Header</strong> se découpe en deux tâches&#160;: <strong>HeaderTask</strong> et <strong>HeaderPhpTask</strong>.</p>

<p>La tâche <strong>HeaderTask</strong> permet d&#8217;ajouter un en-tête à n&#8217;importe quel fichier. Elle peut être comparée au paramètre imbriqué <em>header</em> de la tâche <a href="http://ant.apache.org/manual/CoreTasks/concat.html" title="Concat">Concat</a> d&#8217;<a href="http://ant.apache.org/" title="Apache Ant - Welcome">Ant</a><sup id="fnref:2"><a href="#fn:2" rel="footnote">2</a></sup>.</p>

<p>Plus spécifique, la tâche <strong>HeaderPhpTask</strong> est à utiliser pour ajouter un en-tête aux fichiers PHP. Le tag PHP <code>&lt;?php</code> précédera ainsi le contenu à concaténer&#160;; le premier tag PHP étant supprimé.
<span id="more-215"></span></p>

<h2 id="toc">Table des matières</h2>

<ol>
<li><a href="#presentation">Présentation</a></li>
<li><a href="#telecharger">Téléchargement</a></li>
<li><a href="#installation">Installation</a></li>
<li><a href="#attributs">Attributs</a></li>
<li><a href="#nested-tags">Tags imbriquées supportées</a></li>
<li><a href="#utilisation">Exemples d&#8217;utilisation</a></li>
<li><a href="#licence">Licence</a></li>
<li><a href="#todo">Tâches à faire</a></li>
<li><a href="#changelog">Historique des versions et changelog</a></li>
</ol>

<h2 id="telecharger">Téléchargement</h2>

<div class="download box">
    <a href="http://cloud.github.com/downloads/piouPiouM/phing-header/phing-header-1.0.zip" title="Téléchargement depuis les serveurs de GitHub"><span>Télécharger la dernière version de la tâche <strong>Phing Header</strong> (8&#160;Ko)</span></a>
</div>

<p>Vous pouvez également cloner le projet depuis son <a href="http://github.com/piouPiouM/phing-header/" title="piouPiouM's phing-header at master - GitHub">dépôt GitHub</a>.</p>

<h2 id="installer">Installation</h2>

<p>Si vous suivez la <a href="http://phing.info/docs/guide/current/chapters/ExtendingPhing.html#CreatingATask" title="Phing Guide - Extending Phing">documentation</a> de Phing, il vous faut copier le répertoire<sup id="fnref:3"><a href="#fn:3" rel="footnote">3</a></sup> <code>header</code> contenu dans l&#8217;archive dans le dossier <code>classes/phing/tasks/my</code> de votre installation Phing.</p>

<p>Personnellement je préfère utiliser un chemin externe à l&#8217;installation de base de Phing. Pour ce faire, il suffit de créer l&#8217;arborescence <code>phing/tasks/my</code> à l&#8217;endroit désiré (par exemple <code>/Library/WebServer/share/php</code>) et de déclarer ce dernier <em>via</em> la propriété <code>include_path</code> de votre fichier <code>php.ini</code>. Ce qui nous donne ici&#160;:</p>


<div class="wp_syntax"><div class="code"><pre class="ini" style="font-family:monospace;"><span style="color: #000099;">include_path</span> <span style="color: #000066; font-weight:bold;">=</span> <span style="color: #933;">&quot;.:/Library/WebServer/share/php:/usr/local/php5/lib/php&quot;</span></pre></div></div>


<p>et l&#8217;arborescence suivante&#160;:</p>


<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">/Library/WebServer/share/php/
  `-- phing/
       `-- tasks/
            `-- my/
                 `-- header/
                      |-- HeaderPhpTask.php
                      `-- HeaderTask.php</pre></div></div>


<p>Vous devrez déclarer ces nouvelles tâches à l&#8217;aide de la tâche <a href="http://phing.info/docs/guide/current/chapters/appendixes/AppendixB-CoreTasks.html#TaskdefTask" title="Phing Guide - Core Tasks">Taskdef</a> dans vos fichiers de build. Par exemple&#160;:</p>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;taskdef</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;header&quot;</span> <span style="color: #000066;">classname</span>=<span style="color: #ff0000;">&quot;phing.tasks.my.header.HeaderTask&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>


<h2>Attributs</h2>

<p>Les tâches <strong>HeaderTask</strong> et <strong>HeaderPhpTask</strong> se partagent la liste d&#8217;attributs qui suit&#160;:</p>

<table>
<thead>
<tr>
  <th>Nom</th>
  <th>Type</th>
  <th>Description</th>
  <th align="center">Defaut</th>
  <th align="center">Requis</th>
</tr>
</thead>
<tbody>
<tr>
  <td>file</td>
  <td>String</td>
  <td>Le chemin du fichier en-tête à charger.</td>
  <td align="center">n/a</td>
  <td align="center">Oui</td>
</tr>
<tr>
  <td>eol</td>
  <td>String</td>
  <td>Préciser le <a href="#eol">caractère de fin de ligne</a>.</td>
  <td align="center"><code>PHP_EOL</code></td>
  <td align="center">Non</td>
</tr>
<tr>
  <td>preservelastmodified</td>
  <td>Boolean</td>
  <td>Conserver la date de dernière modification de tous les fichiers.</td>
  <td align="center">false</td>
  <td align="center">Non</td>
</tr>
<tr>
  <td>failonerror</td>
  <td>Boolean</td>
  <td>Stopper le processus à la première erreur survenue.</td>
  <td align="center">true</td>
  <td align="center">Non</td>
</tr>
<tr>
  <td>verbose</td>
  <td>Boolean</td>
  <td>Activer le mode verbeu.</td>
  <td align="center">false</td>
  <td align="center">Non</td>
</tr>
<tr>
  <td>encoding</td>
  <td>String</td>
  <td>Préciser l&#8217;encodage du fichier en-tête.</td>
  <td align="center">UTF-8</td>
  <td align="center">Non</td>
</tr>
<tr>
  <td>toencoding</td>
  <td>String</td>
  <td>Préciser l&#8217;encodage des fichiers de destination.</td>
  <td align="center">UTF-8</td>
  <td align="center">Non</td>
</tr>
</tbody>
</table>

<h3 id="eol">Caractères de fin de ligne valides</h3>

<ul>
<li>cr&#160;: un unique CR.</li>
<li>lf&#160;: un unique LF.</li>
<li>crlf&#160;: la paire CRLF.</li>
<li>mac&#160;: un unique CR.</li>
<li>unix&#160;: un unique LF.</li>
<li>dos&#160;: la paire CRLF.</li>
</ul>

<p>La valeur par défaut est dépendante de la plateforme utilisée. Pour les système Unix, le caractère de fin de ligne par défaut est <code>LF</code>. Pour les systèmes de types DOS (incluant Windows), il s&#8217;agit de <code>CRLF</code>. Enfin, <code>CR</code> est la caractère utilisé par défaut sur les systèmes Mac OS.</p>

<h2 id="nested-tags">Tags imbriquées supportées</h2>

<ul>
<li><a href="http://phing.info/docs/guide/current/chapters/appendixes/AppendixD-CoreTypes.html#Fileset" title="Phing Guide - Core Types">FileSet</a>.</li>
</ul>

<h2 id="utilisation">Exemples d&#8217;utilisation</h2>

<h3>Ajouter un en-tête à un fichier PHP</h3>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- load the header php task --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;taskdef</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;phpheader&quot;</span> <span style="color: #000066;">classname</span>=<span style="color: #ff0000;">&quot;phing.tasks.my.header.HeaderPhpTask&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;phpheader</span></span>
<span style="color: #009900;">    <span style="color: #000066;">file</span>=<span style="color: #ff0000;">&quot;${project.basedir}/LICENSE&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">tofile</span>=<span style="color: #ff0000;">&quot;${project.basedir}/build/foo.php&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/phpheader<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>


<h3>Ajouter un en-tête à un ensemble de fichiers PHP</h3>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- load the header php task --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;taskdef</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;phpheader&quot;</span> <span style="color: #000066;">classname</span>=<span style="color: #ff0000;">&quot;phing.tasks.my.header.HeaderPhpTask&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;phpheader</span> <span style="color: #000066;">file</span>=<span style="color: #ff0000;">&quot;${project.basedir}/LICENSE&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;fileset</span> <span style="color: #000066;">dir</span>=<span style="color: #ff0000;">&quot;${project.basedir}/build&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;include</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;**/*.php&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;exclude</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;**/foo*.php&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/fileset<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/phpheader<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>


<h3>Ajouter un en-tête encodé en ISO-8859-1 à un fichier encodé en UTF-8</h3>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">&lt;!-- load the header task --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;taskdef</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;header&quot;</span> <span style="color: #000066;">classname</span>=<span style="color: #ff0000;">&quot;phing.tasks.my.header.HeaderTask&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;header</span></span>
<span style="color: #009900;">    <span style="color: #000066;">file</span>=<span style="color: #ff0000;">&quot;${project.basedir}/LICENSE&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;ISO-8859-1&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">tofile</span>=<span style="color: #ff0000;">&quot;${project.basedir}/build/foo.txt&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span></pre></div></div>


<h2 id="licence">Licence</h2>

<p>Copyright (C) 2008 Mehdi Kabab, distribué sous licence <a href="http://www.gnu.org/licenses/lgpl.html" title="GNU Lesser General Public License - GNU Project - Free Software Foundation (FSF)"><abbr title="GNU Lesser Public General License">LGPL</abbr></a> version 3 ou supérieure.</p>

<h2 id="todo">Tâches à faire</h2>

<p>Fonctionnalités en attente de développement.</p>

<ul>
<li>[HeaderPhpTask] Optimiser la détection des tags PHP.</li>
<li>[Projet] Améliorer l&#8217;aspect POO.</li>
</ul>

<h2 id="changelog">Historique des versions et changelog</h2>

<h3>1.0</h3>

<ul>
<li>Version initiale.</li>
</ul>

<h3 class='related_post_title'>Continuez votre lecture sur des sujets similaires</h3>

<ul class='related_post'><li><a href='http://pioupioum.fr/snippets/ant-bookmarklet-macrodef.html' title='Générer des bookmarklets via Ant'>Générer des bookmarklets via Ant</a></li><li><a href='http://pioupioum.fr/outils-astuces/license-helper-textmate-bundle.html' title='Bundle License Helper pour TextMate'>Bundle License Helper pour TextMate</a></li><li><a href='http://pioupioum.fr/snippets/textmate-commande-exporter-mots-word-wrap.html' title='TextMate : commande &laquo;&nbsp;Export words&nbsp;&raquo;'>TextMate&#160;: commande &laquo;&nbsp;Export words&nbsp;&raquo;</a></li><li><a href='http://pioupioum.fr/snippets/php-installer-apc-macosx.html' title='PHP : installer APC sous Mac OS X Leopard'>PHP&#160;: installer APC sous Mac OS X Leopard</a></li><li><a href='http://pioupioum.fr/snippets/php-convertir-datetime-unix-timestamp.html' title='PHP : convertir un DATETIME en un timestamp UNIX'>PHP&#160;: convertir un DATETIME en un timestamp UNIX</a></li></ul>

<div class="footnotes">
<hr />
<ol>

<li id="fn:1">
<p>nous travaillons par conséquent sur des projets PHP. Cependant, la tâche est utilisable pour tout type de fichier non binaire.&#160;<a href="#fnref:1" rev="footnote">&#8617;</a></p>
</li>

<li id="fn:2">
<p>la tâche <em>Concat</em> n&#8217;est malheureusement pas encore disponible dans Phing.&#160;<a href="#fnref:2" rev="footnote">&#8617;</a></p>
</li>

<li id="fn:3">
<p>ou directement les fichiers <code>HeaderTask.php</code> et <code>HeaderPhpTask.php</code>. Il vous suffira d&#8217;adapter en conséquence le chemin d&#8217;accès lors de leur déclaration <em>via</em> <code>taskdef</code>.&#160;<a href="#fnref:3" rev="footnote">&#8617;</a></p>
</li>

</ol>
</div>
<!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://pioupioum.fr/developpement/phing-header-task.html/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP&#160;: supprimer récursivement un répertoire</title>
		<link>http://pioupioum.fr/developpement/php-spl-supprimer-repertoire-recursif.html</link>
		<comments>http://pioupioum.fr/developpement/php-spl-supprimer-repertoire-recursif.html#comments</comments>
		<pubDate>Fri, 17 Jul 2009 17:47:02 +0000</pubDate>
		<dc:creator>piouPiouM</dc:creator>
				<category><![CDATA[Développement Web]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[RecursiveDirectoryIterator]]></category>
		<category><![CDATA[RecursiveIteratorIterator]]></category>
		<category><![CDATA[SPL]]></category>

		<guid isPermaLink="false">http://pioupioum.fr/?p=95</guid>
		<description><![CDATA[Voici une petite fonction pour supprimer un répertoire et toute son arborescence. J&#8217;utilise ici la Standard PHP Library (SPL) et notamment son itérateur RecursiveDirectoryIterator qui permet de traverser récursivement un répertoire.

Il est important de noter l&#8217;utilisation de la constante RecursiveIteratorIterator::CHILD_FIRST. Grâce à elle, l&#8217;itérateur, instance de la classe RecursiveIteratorIterator, va parcourir récursivevement les fichiers des [...]]]></description>
			<content:encoded><![CDATA[<!-- google_ad_section_start --><p>Voici une petite fonction pour supprimer un répertoire et toute son arborescence. J&#8217;utilise ici la <a href="http://fr.php.net/spl" title="PHP: SPL - Manual">Standard PHP Library</a> (SPL) et notamment son itérateur <strong>RecursiveDirectoryIterator</strong> qui permet de traverser récursivement un répertoire.</p>

<p>Il est important de noter l&#8217;utilisation de la constante <code>RecursiveIteratorIterator::CHILD_FIRST</code>. Grâce à elle, l&#8217;itérateur, instance de la classe <strong>RecursiveIteratorIterator</strong>, va parcourir récursivevement les fichiers des dossiers. Après avoir supprimés les fichiers, l&#8217;itérateur passe au dossier parent qui pourra être à son tour détruit. En effet, la fonction <a href="http://fr.php.net/manual/fr/function.rmdir.php" title="PHP: rmdir - Manual"><code>rmdir</code></a> ne peut qu&#8217;effacer un dossier vide.
<span id="more-95"></span></p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #009933; font-style: italic;">/**
 * Attempts to remove recursively the directory named by dirname.
 *
 * @author Mehdi Kabab &lt;http://pioupioum.fr&gt;
 * @copyright Copyright (C) 2009 Mehdi Kabab
 * @license http://www.gnu.org/licenses/gpl.html  GNU GPL version 3 or later
 *
 * @param string $dirname Path to the directory.
 * @param boolean $followLinks Removes symbolic links if set to TRUE.
 * @return boolean Returns TRUE on success or FALSE on failure.
 * @throws Exception
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> recursive_rmdir<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dirname</span><span style="color: #339933;">,</span> <span style="color: #000088;">$followLinks</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_dir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dirname</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #990000;">is_link</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dirname</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_writable</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dirname</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'You do not have renaming permissions!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$iterator</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> RecursiveIteratorIterator<span style="color: #009900;">&#40;</span>
            <span style="color: #000000; font-weight: bold;">new</span> RecursiveDirectoryIterator<span style="color: #009900;">&#40;</span><span style="color: #000088;">$dirname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            RecursiveIteratorIterator<span style="color: #339933;">::</span><span style="color: #004000;">CHILD_FIRST</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">valid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isDot</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
            <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isWritable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                    throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span>
                        <span style="color: #0000ff;">'Permission Denied: %s.'</span><span style="color: #339933;">,</span>
                        <span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPathName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                    <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isLink</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">===</span> <span style="color: #009900;">&#40;</span>boolean<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$followLinks</span><span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isFile</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #990000;">unlink</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPathName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">isDir</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #009900;">&#123;</span>
                    <span style="color: #990000;">rmdir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPathName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
            <span style="color: #009900;">&#125;</span>
&nbsp;
            <span style="color: #000088;">$iterator</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$iterator</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Fix for Windows.</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">rmdir</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$dirname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span>
    <span style="color: #009900;">&#123;</span>
        throw <span style="color: #000000; font-weight: bold;">new</span> Exception<span style="color: #009900;">&#40;</span><span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Directory %s does not exist!'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$dirname</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></td></tr></table></div>


<p><strong>Mis à jour du 02/08/2009</strong><br />
Suppression de la levée d&#8217;exception sur la suppression du lien symbolique. Le script passe au fichier suivant. Merci à <a href="#comment-21" title="Voir le commentaire">Oncle Tom</a>.</p>

<p><strong>Mis à jour du 03/08/2009</strong><br />
Patch pour Windows. Libération de l&#8217;itérateur qui ferme correctement le répertoire à supprimer. Merci à <a href="#comment-30" title="Voir le commentaire">Yho</a>.</p>

<h3 class='related_post_title'>Continuez votre lecture sur des sujets similaires</h3>

<ul class='related_post'><li><a href='http://pioupioum.fr/snippets/textmate-commande-exporter-mots-word-wrap.html' title='TextMate : commande &laquo;&nbsp;Export words&nbsp;&raquo;'>TextMate&#160;: commande &laquo;&nbsp;Export words&nbsp;&raquo;</a></li><li><a href='http://pioupioum.fr/snippets/php-installer-apc-macosx.html' title='PHP : installer APC sous Mac OS X Leopard'>PHP&#160;: installer APC sous Mac OS X Leopard</a></li><li><a href='http://pioupioum.fr/snippets/php-convertir-datetime-unix-timestamp.html' title='PHP : convertir un DATETIME en un timestamp UNIX'>PHP&#160;: convertir un DATETIME en un timestamp UNIX</a></li><li><a href='http://pioupioum.fr/snippets/php-uncamel-fonction-convertir-camel-case.html' title='PHP : fonction uncamel() pour inverser une notation en CamelCase'>PHP&#160;: fonction uncamel() pour inverser une notation en CamelCase</a></li><li><a href='http://pioupioum.fr/snippets/php-valider-url-domaine.html' title='PHP : valider qu&#8217;une URL pointe sur un domaine donné'>PHP&#160;: valider qu&#8217;une URL pointe sur un domaine donné</a></li></ul>
<!-- google_ad_section_end -->]]></content:encoded>
			<wfw:commentRss>http://pioupioum.fr/developpement/php-spl-supprimer-repertoire-recursif.html/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>
