Ouzo\Utilities\Strings::sprintAssoc PHP Method

sprintAssoc() public static method

Example: $sprintfString = "This is %{what}! %{what}? This is %{place}!"; $assocArray = array( 'what' => 'madness', 'place' => 'Sparta' ); Result: 'This is madness! madness? This is Sparta!'
public static sprintAssoc ( string $string, array $params ) : string
$string string
$params array
return string
    public static function sprintAssoc($string, $params)
    {
        foreach ($params as $key => $value) {
            $string = preg_replace("/%{({$key})}/", $value, $string);
        }
        return $string;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * @test
  */
 public function shouldSprintfStringWithAssocArrayAsParam()
 {
     //given
     $sprintfString = "This is %{what}! %{what}? This is %{place}!";
     $assocArray = array('what' => 'madness', 'place' => 'Sparta');
     //when
     $resultString = Strings::sprintAssoc($sprintfString, $assocArray);
     //then
     $this->assertEquals('This is madness! madness? This is Sparta!', $resultString);
 }
All Usage Examples Of Ouzo\Utilities\Strings::sprintAssoc