PO::poify( string $input_string ): string

In this article

Formats a string in PO-style

Parameters

$input_stringstringrequired
the string to format

Return

string the poified string

Source

public static function poify( $input_string ) {
	$quote   = '"';
	$slash   = '\\';
	$newline = "\n";

	$replaces = array(
		"$slash" => "$slash$slash",
		"$quote" => "$slash$quote",
		"\t"     => '\t',
	);

	$input_string = str_replace( array_keys( $replaces ), array_values( $replaces ), $input_string );

	$po = $quote . implode( "{$slash}n{$quote}{$newline}{$quote}", explode( $newline, $input_string ) ) . $quote;
	// Add empty string on first line for readability.
	if ( str_contains( $input_string, $newline ) &&
		( substr_count( $input_string, $newline ) > 1 || substr( $input_string, -strlen( $newline ) ) !== $newline ) ) {
		$po = "$quote$quote$newline$po";
	}
	// Remove empty strings.
	$po = str_replace( "$newline$quote$quote", '', $po );
	return $po;
}

User Contributed Notes

You must log in before being able to contribute a note or feedback.