I am trying to make a class that can process strings with un defined variables. Is this possible? or is there a better way?
For instance, if I have the following and would like this class Looper to take $str_1 and output it with the variables $fname and $lname filled out … then somewhere else i can reuse Looper class and process $str_2 since they both need $fname and $lname.
class Looper {
public function processLoop($str){
$s='';
$i=0;
while ($i < 4){
$fname = 'f' . $i;
$lname = 'l' . $i;
$s .= $str . '<br />';
$i++;
}
return $s;
}
}
$str_1 = "First Name: $fname, Last Name: $lname";
$rl = new Looper;
print $rl->processLoop($str_1);
$str_2 = "Lorem Ipsum $fname $lname is simply dummy text of the printing and typesetting industry";
print $rl->processLoop($str_2);
Why not just use
strtr:So in the context if your class:
Likewise if you dont want to be dependent on named placeholders you could use positional placeholders via
sprintf:In that case your
$strargument would look like"First Name: %s, Last Name: %s"So over all usage: