$txt : "Comment trouver et capturer les déterminants le le les de des etc... suivis d'un mot (avec capture du mot) dans le texte suivant:
La syntaxe des masques utilisés dans ces fonctions ressemble fort à celle de Perl. Les expressions seront entourées de délimiteurs, slash (/), par exemple. N'importe quel caractère peut servir de délimiteur, tant qu'il n'est pas alpha-numérique ou n'est pas un anti-slash (\). Si un délimiteur doit être utilisé dans l'expression, il faudra l'échapper avec un anti-slash. Depuis PHP 4.0.4, vous pouvez utiliser les délimiteurs (), {}, [], et <>, comme en Perl. Voir la syntaxe des masques pour plus d'explications.
$pat1typ : preg_match_all
$pat1 : #\b[ld][aeu]s?\s(\w+)\b#
$pat2typ : preg_match_all
$pat2 :
$pat3 :
$result : Hig
motif nº1 : preg_match_all('#\b[ld][aeu]s?\s(\w+)\b#', $txt)
"Comment trouver et capturer les déterminants lele les de des etc... suivis d'un mot (avec capture du mot) dans le texte suivant:
La syntaxe des masques utilisés dans ces fonctions ressemble fort à celle de Perl. Les expressions seront entourées e ddélimiteurs, slash (/), par exemple. N'importe quel caractère peut servir e ddélimiteur, tant qu'il n'est pas alpha-numérique ou n'est pas un anti-slash (\). Si un délimiteur doit être utilisé dans l'expression, il faudra l'échapper avec un anti-slash. Depuis PHP 4.0.4, vous pouvez utiliser les délimiteurs (), {}, [], et <>, comme en Perl. Voir la syntaxe des masques pour plus d'explications.
code source de la page :
<?php
header('Content-type: text/html; charset=UTF-8');
ini_set('magic_quotes_gpc', 0);
if($_SERVER['REQUEST_METHOD'] === 'POST')
{
$txt = htmlentities(stripslashes($_POST['txt']));
$pat1typ = htmlentities(stripslashes($_POST['pat1typ']));
$pat1 = htmlentities(stripslashes($_POST['pat1']));
$pat2typ = htmlentities(stripslashes($_POST['pat2typ']));
$pat2 = htmlentities(stripslashes($_POST['pat2']));
$pat3 = htmlentities(stripslashes($_POST['pat3']));
$result = htmlentities(stripslashes($_POST['result']));
}
else {
$txt = '"Comment trouver et capturer les déterminants le le les de des etc... suivis d\'un mot (avec capture du mot) dans le texte suivant:
La syntaxe des masques utilisés dans ces fonctions ressemble fort à celle de Perl. Les expressions seront entourées de délimiteurs, slash (/), par exemple. N\'importe quel caractère peut servir de délimiteur, tant qu\'il n\'est pas alpha-numérique ou n\'est pas un anti-slash (\). Si un délimiteur doit être utilisé dans l\'expression, il faudra l\'échapper avec un anti-slash. Depuis PHP 4.0.4, vous pouvez utiliser les délimiteurs (), {}, [], et <>, comme en Perl. Voir la syntaxe des masques pour plus d\'explications. ';
$pat1typ = 'preg_match_all';
$pat1 = '#\b[ld][aeu]s?\s(\w+)\b#';
$pat2typ = 'preg_match_all';
$pat2 = '';
$pat3 = '';
$result = 'Hig';
};
function regex($pattyp, $pat, $txt){
$pattyp = html_entity_decode($pattyp);
$pat = html_entity_decode($pat);
$txt = html_entity_decode($txt);
$pattyp($pat, $txt, $out);
return $out;
}
function surl($masque){
$mask = preg_replace('\''.$masque[1].'\'', '', $masque[0], 1); //problème avec les mots doublé ex : "le le" devient "lele !"
return '<span style=\'background-color:yellow;\'>'.$mask.'<u>'.$masque[1].'</u></span>';
}
function surligne($pattyp, $pat, $txt){
$pattyp = html_entity_decode($pattyp);
$pat = html_entity_decode($pat);
$txt = html_entity_decode($txt);
switch($pattyp){
case 'preg_match':
$out = preg_replace_callback($pat, 'surl', $txt, 1);
return htmlentities($out);
break;
case 'preg_match_all':
$out = preg_replace_callback($pat, 'surl', $txt);
return htmlentities($out);
break;
case 'ereg':
$out = ereg_replace($pat, '<span style=\'background-color:yellow;\'>\0</span>', $txt);
return htmlentities($out);
break;
}
}
function mode_remplace($pattyp){
$pattyp = html_entity_decode($pattyp);
switch($pattyp){
case 'preg_match':
case 'preg_match_all':
$out = 'preg_replace';
return $out;
break;
case 'ereg':
$out = 'ereg_replace';
return $out;
break;
}
}
function remplace($pattyp, $pat, $replace, $txt){
$pattyp = html_entity_decode($pattyp);
$pat = html_entity_decode($pat);
$replace = html_entity_decode($replace);
$txt = html_entity_decode($txt);
$a = mode_remplace($pattyp);
$out = $a($pat, $replace, $txt);
return htmlentities($out);
}
function chrono($pattyp, $pat, $txt){
$pattyp = html_entity_decode($pattyp);
$pat = html_entity_decode($pat);
$txt = html_entity_decode($txt);
$start = microtime(true);
for($i = 0; $i < 1000; $i++) {
$pattyp($pat, $txt, $out);
}
$end = microtime(true);
$out = round(1000*($end - $start), 3);
return $out;
}
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//fr" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'."\n\n";
echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr" lang="fr">'."\n".'<head>'."\n".'<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />'."\n".'<title>test de regex</title>'."\n".'</head>'."\n".'<body>'."\n\n";
echo "\$txt : $txt <br />";
echo "\$pat1typ : $pat1typ <br />";
echo "\$pat1 : $pat1 <br />";
echo "\$pat2typ : $pat2typ <br />";
echo "\$pat2 : $pat2 <br />";
echo "\$pat3 : $pat3 <br />";
echo "\$result : $result <br />";
echo '<div id="testeur">'."\n\n";
echo '<form id="formulaire" action="regex.php" method="post">'."\n\n";
echo '<label for="txt"><strong>texte à tester :</strong></label><br />'."\n";
echo '<textarea id="txt" name="txt" rows="5" cols="90">'.html_entity_decode($txt).'</textarea>'."\n".'<br />'."\n\n";
echo '<label for="pat1"><strong>motif nº1 :</strong></label>'."\n".'<br />'."\n";
echo '<input type="radio" id="pat1radio1" name="pat1typ" value="preg_match"';
if(html_entity_decode($pat1typ) === 'preg_match')
{
echo ' checked="checked"';
}
echo ' /><label for="pat1radio1">preg_match</label>'."\n".'<input type="radio" id="pat1radio2" name="pat1typ" value="preg_match_all"';
if(html_entity_decode($pat1typ) === 'preg_match_all')
{
echo " checked=\"checked\"";
}
echo ' /><label for="pat1radio2">preg_match_all</label>'."\n".'<input type="radio" id="pat1radio3" name="pat1typ" value="ereg"';
if(html_entity_decode($pat1typ) === 'ereg')
{
echo ' checked="checked"';
}
echo ' /><label for="pat1radio3">ereg</label>'."\n".'<br />'."\n\n";
echo '<input type="text" id="pat1" name="pat1" value="'.html_entity_decode($pat1).'" size="85" />'."\n".'<br />'."\n\n";
echo '<label for="pat2"><strong>motif nº2 :</strong></label>'."\n".'<br />'."\n";
echo '<input type="radio" id="pat2radio1" name="pat2typ" value="preg_match"';
if(html_entity_decode($pat2typ) === 'preg_match')
{
echo ' checked="checked"';
}
echo ' /><label for="pat2radio1">preg_match</label>'."\n".'<input type="radio" id="pat2radio2" name="pat2typ" value="preg_match_all"';
if(html_entity_decode($pat2typ) === 'preg_match_all')
{
echo ' checked="checked"';
}
echo ' /><label for="pat2radio2">preg_match_all</label>'."\n".'<input type="radio" id="pat2radio3" name="pat2typ" value="ereg"';
if(html_entity_decode($pat2typ) === 'ereg')
{
echo ' checked="checked"';
}
echo ' /><label for="pat2radio3">ereg</label>'."\n".'<br />'."\n\n";
echo '<input type="text" id="pat2" name="pat2" value="'.html_entity_decode($pat2).'" size="85" />'."\n".'<br />'."\n\n";
echo '<label for="pat3"><strong>motif pour le replace :</strong></label>'."\n".'<br />'."\n";
echo '<input type="text" id="pat3" name="pat3" value="'.html_entity_decode($pat3).'" size="85" />'."\n".'<br />'."\n\n";
echo '<label for="resultradio1"><strong>affichage des résultats :</strong></label>'."\n".'<br />'."\n";
echo '<input type="radio" id="resultradio1" name="result" value="Hig"';
if(html_entity_decode($result) === 'Hig')
{
echo ' checked="checked"';
}
echo ' /><label for="resultradio1">Highlight</label>'."\n".'<input type="radio" id="resultradio2" name="result" value="Tab"';
if(html_entity_decode($result) === 'Tab')
{
echo ' checked="checked"';
}
echo ' /><label for="resultradio2">Tableau</label>'."\n".'<input type="radio" id="resultradio3" name="result" value="Tim"';
if(html_entity_decode($result) ==='Tim')
{
echo ' checked="checked"';
}
echo ' /><label for="resultradio3">Timing</label>'."\n".'<br />'."\n\n";
echo '<input name="bouton" type="submit" value="Tester" />'."\n\n";
echo '</form>'."\n\n";
echo '</div>'."\n\n";
if(html_entity_decode($pat1) != NULL){
echo '<div id="result1">'."\n\n";
echo '<span><strong>motif nº1 : </strong>'.html_entity_decode($pat1typ).'(\''.html_entity_decode($pat1).'\', $txt)</span>'."\n".'<br />'."\n\n";
if(html_entity_decode($result) === 'Hig'){
$test = surligne(html_entity_decode($pat1typ), html_entity_decode($pat1), html_entity_decode($txt));
echo html_entity_decode($test)."\n";
if(html_entity_decode($pat3) != NULL){
$test = mode_remplace(html_entity_decode($pat1typ));
echo '<br /><span><strong>remplacement : </strong>'.html_entity_decode($test).'(\''.html_entity_decode($pat1).'\', \''.html_entity_decode($pat3).'\', $txt)</span>'."\n".'<br />'."\n";
$test = remplace(html_entity_decode($pat1typ), html_entity_decode($pat1), html_entity_decode($pat3), html_entity_decode($txt));
echo html_entity_decode($test)."\n";
}
}
elseif(html_entity_decode($result) === 'Tab'){
echo '<pre>'."\n";
$test = regex($pat1typ, $pat1, $txt);
print_r($test);
echo '</pre>'."\n";
}
elseif(html_entity_decode($result) === 'Tim'){
$test = chrono($pat1typ, $pat1, $txt);
echo 'durée : '.$test.' ms pour 1000 occurrences'."\n";
}
echo "\n".'</div>'."\n\n";
}
if(html_entity_decode($pat2) != NULL){
echo '<div id="result2">'."\n\n";
echo '<span><strong>motif nº2 : </strong>'.html_entity_decode($pat2typ).'(\''.html_entity_decode($pat2).'\', $txt)</span>'."\n".'<br />'."\n\n";
if(html_entity_decode($result) === 'Hig'){
$test = surligne($pat2typ, $pat2, $txt);
echo html_entity_decode($test)."\n";
if(html_entity_decode($pat3) != NULL){
$test = mode_remplace($pat2typ);
echo '<br /><span><strong>remplacement : </strong>'.html_entity_decode($test).'(\''.html_entity_decode($pat2).'\', \''.html_entity_decode($pat3).'\', $txt)</span>'."\n".'<br />'."\n";
$test = remplace($pat2typ, $pat2, $pat3, $txt);
echo html_entity_decode($test)."\n";
}
}
elseif(html_entity_decode($result) === 'Tab'){
echo '<pre>'."\n";
$test = regex($pat2typ, $pat2, $txt);
print_r($test);
echo '</pre>'."\n";
}
elseif(html_entity_decode($result) === 'Tim') {
$test = chrono($pat2typ, $pat2, $txt);
echo 'durée : '.$test.' ms pour 1000 occurrences'."\n";
}
echo '</div>'."\n\n";
}
echo '<br />'."\n".'<strong>code source de la page : </strong>'."\n".'<br />'."\n";
echo '<div style="background-color:#DDDDDD;">';
highlight_file(__FILE__);
echo '</div>';
echo '</body>'."\n".'</html>'."\n";
?>