NUR erster Fund ersetzten (str_replace)

edit | delete

Autor: Varsamis Karamanidis (Macky)

eingetragen: Donnerstag, 14. Juni 2018 um 10:58 Uhr (24/2018 Kalenderwoche)

geändert: Donnerstag, 14. Juni 2018 um 10:59 Uhr (24/2018 Kalenderwoche)

Keywords: replace ersetzen preg_quote preg_replace

Kategorien: PHP,

Text:

Ersetzt nur den ersten Fund in einer Zeichenkette

Quellcode:  

/**
 * Durchsucht und ersetzt nur das erste Vorkommen
 *
 * PHP Version 5
 *
 * @param string $from
 * @param string $to
 * @param string $content
 * @return string
 */
public static function str_replace_first($from, $to, $content)
{
	$from = '/'.preg_quote($from, '/').'/';

	return preg_replace($from, $to, $content, 1);
}