Arraywerte in String einsetzen (Beispiel 1)

Eine spezielle Anwendung

Weitere Beispiele

Beispiel ausführen
<?php

$test 
= array(
 
'Affe',
 
'Bär',
 
'Camel',
 
'Dachs',
 
'Elefant',
 
'Fuchs',
 
'Giraffe',
 
'Hermelin',
 
'Igel',
 
'Jaguar',
 
'Koala',
 
'Lama',
 
'Manta',
);



function 
smart_sprintf($tpl$data)
{
    
// Anzahl der Platzhalter ermitteln
    
$PlaceholderInTemplate substr_count($tpl'%s');

    
// Anzahl der Arrayelemente
    
$nbr count($data);

    
$TotalRows ceil($nbr$PlaceholderInTemplate);

    
// Array erweitern, damit die Anzahl aufgeht
    
$TotalNbrOfPlaceholder $TotalRows $PlaceholderInTemplate;

    
// Array erweitern
    
$EmptyValue '-';
    
$data array_pad($data$TotalNbrOfPlaceholder$EmptyValue);

    
$CurrentRow 0;

    while (
$CurrentRow $TotalRows) {

        
$offset $CurrentRow $PlaceholderInTemplate;

        
$part array_slice($data$offset$PlaceholderInTemplate);

        
vprintf($tpl$part);

        
$CurrentRow ++;
    }
}    


smart_sprintf('%s %s %s<br>'$test);

echo 
'<hr>';

smart_sprintf('%s %s <b>%s</b> %s<br>'$test);

echo 
'<hr>';
smart_sprintf('<em>%s</em> %s <strong>%s</strong> %s<br>'$test);

echo 
'<hr>';
smart_sprintf('<em>%s %s</em><br>', array('A','b','III'));


?>

Hinweise zum DatenschutzImpressum © 2005-2008 S. Eickhoff