lithium\util\String::insert PHP Method

insert() public static method

Usage: String::insert( 'My name is {:name} and I am {:age} years old.', array('name' => 'Bob', 'age' => '65') ); // returns 'My name is Bob and I am 65 years old.' Please note that optimization have applied to this method and parts of the code may look like it can refactored or removed but in fact this is part of the applied optimization. Please check the history for this section of code before refactoring
public static insert ( string $str, array $data, array $options = [] ) : string
$str string A string containing variable place-holders.
$data array A key, value array where each key stands for a place-holder variable name to be replaced with value.
$options array Available options are: - `'after'`: The character or string after the name of the variable place-holder (defaults to `}`). - `'before'`: The character or string in front of the name of the variable place-holder (defaults to `'{:'`). - `'clean'`: A boolean or array with instructions for `String::clean()`. - `'escape'`: The character or string used to escape the before character or string (defaults to `'\'`). - `'format'`: A regular expression to use for matching variable place-holders (defaults to `'/(?
return string
    public static function insert($str, array $data, array $options = array())
    {
        $defaults = array('before' => '{:', 'after' => '}', 'escape' => null, 'format' => null, 'clean' => false);
        $options += $defaults;
        $format = $options['format'];
        if ($format === 'regex' || !$format && $options['escape']) {
            $format = sprintf('/(?<!%s)%s%%s%s/', preg_quote($options['escape'], '/'), str_replace('%', '%%', preg_quote($options['before'], '/')), str_replace('%', '%%', preg_quote($options['after'], '/')));
        }
        if (!$format && key($data) !== 0) {
            $replace = array();
            foreach ($data as $key => $value) {
                if (!is_scalar($value)) {
                    if (is_object($value) && method_exists($value, '__toString')) {
                        $value = (string) $value;
                    } else {
                        $value = '';
                    }
                }
                $replace["{$options['before']}{$key}{$options['after']}"] = $value;
            }
            $str = strtr($str, $replace);
            return $options['clean'] ? static::clean($str, $options) : $str;
        }
        if (strpos($str, '?') !== false && isset($data[0])) {
            $offset = 0;
            while (($pos = strpos($str, '?', $offset)) !== false) {
                $val = array_shift($data);
                $offset = $pos + strlen($val);
                $str = substr_replace($str, $val, $pos, 1);
            }
            return $options['clean'] ? static::clean($str, $options) : $str;
        }
        foreach ($data as $key => $value) {
            if (!($key = sprintf($format, preg_quote($key, '/')))) {
                continue;
            }
            $hash = crc32($key);
            $str = preg_replace($key, $hash, $str);
            $str = str_replace($hash, $value, $str);
        }
        if (!isset($options['format']) && isset($options['before'])) {
            $str = str_replace($options['escape'] . $options['before'], $options['before'], $str);
        }
        return $options['clean'] ? static::clean($str, $options) : $str;
    }

Usage Example

Beispiel #1
0
 /**
  * Process incoming messages
  *
  * @param string $data
  * @return string
  */
 public function process($data)
 {
     $responses = $this->_responses;
     $model = $this->_classes['model'];
     $location = null;
     extract($data);
     $words = preg_split("/[\\s]/", $message, 2);
     if ($words[0] != '~weather') {
         return;
     }
     if (!isset($words[1])) {
         return String::insert($responses['missing'], compact('user'));
     }
     $location = $model::find('search', $words[1]);
     if (!$location || isset($location->title)) {
         return String::insert($responses['unknown'], compact('user') + array('location' => $words[1]));
     }
     if (isset($location->location)) {
         $location = $model::find('search', $location->location[0]->name);
         if (!$location || isset($location->title)) {
             return String::insert($responses['unknown'], compact('user') + array('location' => $words[1]));
         }
     }
     $station = $location->nearby_weather_stations->airport->station[0];
     $weather = $model::find('station', (string) $station->icao);
     if (!$weather || isset($weather->title)) {
         return String::insert($responses['unknown'], compact('user') + array('location', $words[1]));
     }
     return String::insert($responses['weather'], compact('user') + array('city' => (string) $station->city, 'state' => (string) $station->state, 'country' => (string) $station->country, 'icao' => (string) $station->icao, 'temperature' => (string) $weather->temperature_string, 'windchill' => (string) $weather->windchill_string, 'wind' => (string) $weather->wind_string));
 }
All Usage Examples Of lithium\util\String::insert