Post by Mickaël WolffPost by CrazyCatAinsi, tu crées une fonction qui appelle ton autre fonction.
Il avait demandé une solution propre ;) Mais c'est vrai que je n'y
avais pas pensé. Bien vu l'artiste.
L'idée ma parue vicieuse à souhait mais il me semblait avoir lu qu'il y
avait un hic:
Create function ne reçoit qu'un seul argument ($matches).
Au sein de la fonction, donc dans le return, seul ce paramètre est connu.
Les autres variables disponibles ne peuvent être utilisées dans la
fonction car elles lui sont inconnues.
Voici un code exemple, tel que je l'écrit:
function convertLinks($matches) {
$pos = mb_strpos($matches[2], '"');
$filename = mb_substr($matches[2], 0, $pos);
$icon = '';
$pos = mb_strrpos($filename, '.');
if ($pos !== false) {
$ext = mb_strtolower(mb_substr($filename, $pos + 1));
if (!in_array($ext, array('gif', 'png', 'jpg', 'jpeg', 'bmp'))) {
$iconFilename = $GLOBALS['filetypes_path'].'icon_'.$ext.'.png';
$icon = ' <img src="'.$GLOBALS['base_url'].$iconFilename.'"';
$icon .= ' title="'.$ext.'" alt="'.$iconFilename.'" /> ';
$link = 'download.php?file='.$matches[2];
} else {
$link = $GLOBALS['doc_path'].$matches[2];
}
}
$result = '';
$result .= $matches[1].$link.$matches[3];
$result .= $matches[4].$icon.$matches[5];
return $result;
}
function adaptLinks1($source, $base_url, $doc_path, $filetypes_path) {
$result = '';
$GLOBALS['base_url'] = $base_url;
$GLOBALS['doc_path'] = $doc_path;
$GLOBALS['filetypes_path'] = $filetypes_path;
$result = preg_replace_callback(
'|(<a href=")(?!/)(?!http://)(?!#)(.*?)(".*?>)(.*?)(</a>)|U',
'convertLinks',
$source);
return $result;
}
/* Mickaël Wolff start */
class functor
{
protected $params ;
public function __construct()
{
$this->params = array() ;
}
public function __set($name, $value)
{
$this->params[$name] = $value;
}
public function call($matches)
{
$pos = mb_strpos($matches[2], '"');
$filename = mb_substr($matches[2], 0, $pos);
$icon = '';
$pos = mb_strrpos($filename, '.');
if ($pos !== false) {
$ext = mb_strtolower(mb_substr($filename, $pos + 1));
if (!in_array($ext, array('gif', 'png', 'jpg', 'jpeg', 'bmp'))) {
$iconFilename = $this->params['filetypes_path'].'icon_'.
$ext.'.png';
$icon = ' <img src="'.$this->params['base_url'].$iconFilename.'"';
$icon .= ' title="'.$ext.'" alt="'.$iconFilename.'" /> ';
$link = 'download.php?file='.$matches[2];
} else {
$link = $this->params['doc_path'].$matches[2];
}
}
$result = '';
$result .= $matches[1].$link.$matches[3];
$result .= $matches[4].$icon.$matches[5];
return $result;
}
}
function adaptLinks2($source, $base_url, $doc_path, $filetypes_path) {
$result = '';
$f = new functor ;
$f->base_url = $base_url;
$f->doc_path = $doc_path;
$f->filetypes_path = $filetypes_path;
$result = preg_replace_callback(
'|(<a href=")(?!/)(?!http://)(?!#)(.*?)(".*?>)(.*?)(</a>)|U',
array($f, 'call'),
$source);
return $result;
}
/* Mickaël Wolff end */
/* CrazyCat start */
function convertLinks3($matches,$base_url, $doc_path, $filetypes_path) {
$pos = mb_strpos($matches[2], '"');
$filename = mb_substr($matches[2], 0, $pos);
$icon = '';
$pos = mb_strrpos($filename, '.');
if ($pos !== false) {
$ext = mb_strtolower(mb_substr($filename, $pos + 1));
if (!in_array($ext, array('gif', 'png', 'jpg', 'jpeg', 'bmp'))) {
$iconFilename = $filetypes_path.'icon_'.$ext.'.png';
$icon = ' <img src="'.$base_url.$iconFilename.'"';
$icon .= ' title="'.$ext.'" alt="'.$iconFilename.'" /> ';
$link = 'download.php?file='.$matches[2];
} else {
$link = $doc_path.$matches[2];
}
}
$result = '';
$result .= $matches[1].$link.$matches[3];
$result .= $matches[4].$icon.$matches[5];
return $result;
}
function adaptLinks3($source, $base_url, $doc_path, $filetypes_path) {
$result = '';
$result = preg_replace_callback(
'|(<a href=")(?!/)(?!http://)(?!#)(.*?)(".*?>)(.*?)(</a>)|U',
create_function(
'$matches',
'return convertLinks3($matches,$base_url, $doc_path,
$filetypes_path);'
),
$source);
return $result;
}
/* CrazyCat end */
function Main() {
$source = <<<EOD
<ul>
<li> Screenshot:
<a href="image.gif" title="image.gif"><img src="v_image.gif"
title="Screenshot" alt="Screenshot" /></a>
</li>
<li> Exe: <a href="exe.zip" title="exe.zip">exe.zip</a></li>
<li> Source: <a href="src.zip" title="src.zip">src.zip</a></li>
</ul>
EOD;
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'."\n";
echo ' "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'."\n";
echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr"
lang="fr">'."\n";
echo ' <head>'."\n";
echo ' <meta http-equiv="Content-Type" content="text/html;
charset=utf-8"/>'."\n";
echo ' <meta http-equiv="Content-Language" content="fr" />'."\n";
echo ' <title>test</title>'."\n";
echo ' </head>'."\n";
echo ' <body>'."\n";
echo '<h1>Source</h1>'."\n";
echo $source."\n";
echo '<h1>Solution actuelle</h1>'."\n";
echo adaptLinks1($source, 'mon/chemin/', 'mes/documents/', 'leurs/
images/')."\n";
echo '<h1>Solution Mickaël Wolff</h1>'."\n";
echo adaptLinks2($source, 'mon/chemin/', 'mes/documents/', 'leurs/
images/')."\n";
echo '<h1>Solution CrazyCat</h1>'."\n";
echo adaptLinks3($source, 'mon/chemin/', 'mes/documents/', 'leurs/
images/')."\n";
echo ' </body>'."\n";
echo '</html>'."\n";
}
Main();
--
Jack.R
http://jack.r.free.fr