Forum CMS Made Simple FR

Version complète : appeler deux fois le même udt!
Vous consultez actuellement la version basse qualité d’un document. Voir la version complète avec le bon formatage.
Citation :#~~~~~ DEBUT BLOC A NE PAS SUPPRIMER ~~~~~
#~ Version du CMS: 1.11.10
#~ Url du site :
#~ Hébergeur / Soft : ovh
#~ Informations Système :
#~~~~~ FIN BLOC A NE PAS SUPPRIMER ~~~~~



bonjour j'ai créer un udt et qui me permet de transformer les liens youtube en iframe ou en vignette, il fonctionne mais je ne peu pas l'appeler deux fois dans la même page !

des idées?

'
Code :
[== Indéfini ==]

function parse_youtube($link){

        $regexstr = '~
            # Match Youtube link and embed code
            (?:                             # Group to match embed codes
                (?:<iframe [^>]*src=")?       # If iframe match up to first quote of src
                |(?:                        # Group to match if older embed
                    (?:<object .*>)?      # Match opening Object tag
                    (?:<param .*</param>)*  # Match all param tags
                    (?:<embed [^>]*src=")?  # Match embed tag to the first quote of src
                )?                          # End older embed code group
            )?                              # End embed code groups
            (?:                             # Group youtube url
                https?:\/\/                 # Either http or https
                (?:[\w]+\.)*                # Optional subdomains
                (?:                         # Group host alternatives.
                youtu\.be/                  # Either youtu.be,
                | youtube\.com              # or youtube.com
                | youtube-nocookie\.com     # or youtube-nocookie.com
                )                           # End Host Group
                (?:\S*[^\w\-\s])?           # Extra stuff up to VIDEO_ID
                ([\w\-]{11})                # $1: VIDEO_ID is numeric
                [^\s]*                      # Not a space
            )                               # End group
            "?                              # Match end quote if part of src
            (?:[^>]*>)?                       # Match any extra stuff up to close brace
            (?:                             # Group to match last embed code
                </iframe>                 # Match the end of the iframe
                |</embed></object>          # or Match the end of the older embed
            )?                              # End Group of last bit of embed code
            ~ix';

        preg_match($regexstr, $link, $matches);

        return $matches[1];

    }
if(isset($params['link']) && $params['link'] !=""){
    $id=parse_youtube($params['link']);
    if($params['action'] =="thumb"){

        echo "<img src='http://img.youtube.com/vi/".$id."/2.jpg'>";

    }else if($params['action'] =="img"){
        echo "<img src='http://img.youtube.com/vi/".$id."/0.jpg'>";
    }else if($params['action'] =="iframe"){
        echo '<iframe width="560" height="315" src="'.$id.'" frameborder="0" allowfullscreen></iframe>';
    }else if($params['action'] =="default" || empty($params['action'])){
        echo $id;
    }
}
avec 2 UDT appelées tu déclare 2 fois la fonction, comportement interdit en PHP

Essai avec 2 udt différentes. La première avec la fonction, appelée en début de gabarit. La seconde avec l'appel à la fonction là ou tu en as besoin.
Il faut également ajouter cette condition si tu as toujours un message d'erreur du type "cannot redeclare function" :
Code :
if (!function_exists(votrefunction)) {...}
Voir http://www.cmsmadesimple.fr/forum/viewtopic.php?id=5203
merci les gars je test vos solutions et je vous tiens au courrant car cette udt est vraiment sympa ^^ !
+1 Jean ta technique fonctionne...

voici l'udt , avec lequel on peut appeler soit une vignette, soit l'image, soit l'iframe de la vidéo. Cela peut-être cool pour une gallery video, un carroussel (avec list2it par exemple) ou encore pour content-perso , un champ d'article etc...
  • {videos link="liensyoutube"} retourne l'id
  • {videos link="liensyoutube" action="default"}retourne l'id
  • {videos link="liensyoutube" action="thumb"}retourne une vignette
  • {videos link="liensyoutube" action="img"}retourne l'image
  • {videos link="liensyoutube" action="iframe"}retourne un iframe

Code :
[== PHP ==]
if (!function_exists(parse_youtube)) {
    function parse_youtube($link){

        $regexstr = '~
            # Match Youtube link and embed code
            (?:                             # Group to match embed codes
                (?:<iframe [^>]*src=")?       # If iframe match up to first quote of src
                |(?:                        # Group to match if older embed
                    (?:<object .*>)?      # Match opening Object tag
                    (?:<param .*</param>)*  # Match all param tags
                    (?:<embed [^>]*src=")?  # Match embed tag to the first quote of src
                )?                          # End older embed code group
            )?                              # End embed code groups
            (?:                             # Group youtube url
                https?:\/\/                 # Either http or https
                (?:[\w]+\.)*                # Optional subdomains
                (?:                         # Group host alternatives.
                youtu\.be/                  # Either youtu.be,
                | youtube\.com              # or youtube.com
                | youtube-nocookie\.com     # or youtube-nocookie.com
                )                           # End Host Group
                (?:\S*[^\w\-\s])?           # Extra stuff up to VIDEO_ID
                ([\w\-]{11})                # $1: VIDEO_ID is numeric
                [^\s]*                      # Not a space
            )                               # End group
            "?                              # Match end quote if part of src
            (?:[^>]*>)?                       # Match any extra stuff up to close brace
            (?:                             # Group to match last embed code
                </iframe>                 # Match the end of the iframe
                |</embed></object>          # or Match the end of the older embed
            )?                              # End Group of last bit of embed code
            ~ix';

        preg_match($regexstr, $link, $matches);

        return $matches[1];

    }
}
if(isset($params['link']) && $params['link'] !=""){
    $id=parse_youtube($params['link']);
    if($params['action'] =="thumb"){

        echo "<img src='http://img.youtube.com/vi/".$id."/2.jpg'>";

    }else if($params['action'] =="img"){
        echo "<img src='http://img.youtube.com/vi/".$id."/0.jpg'>";
    }else if($params['action'] =="iframe"){
        echo '<iframe width="560" height="315" src="//www.youtube.com/embed/'.$id.'" frameborder="0" allowfullscreen></iframe>';
    }else if($params['action'] =="default" || empty($params['action'])){
        echo $id;
    }
}

désolé par contre l'expression régulière n'est pas de moi et je trouve plus la source