Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Comparing two string modification functions
1. Comparing two string
modification functions
Differences between behaviors of strtr() and
str_replace()
Patrick Maynard | Ixopay
PHPDAY VERONA 2024
1
2. About me
– Coming from the Symfony world
– Now working remotely for Ixopay, a payment orchestrator based in Vienna
– Ixopay uses Laravel, which I am still relatively new to
– We’re hiring! https://www.ixopay.com/en/company/careers
– Social media links: https://patrickmaynard.com
2
3. An introduction to str_replace()
– Replaces substrings, via multiple passes if needed
– Good for real-world scenarios that are unpredictable, but where regexes are
overkill
– Case-insensitive variant is str_ireplace()
3
4. str_replace() example one
– This example uses the method’s simple format, specifying exactly one
replacement pair.
$adjective = 'schön';
$converted = str_replace('ö', 'oe', $adjective);
//Gives us 'schoen'
4
5. str_replace() example two
– Can use an array to specify more complex replacements
$streetName = 'Schönwald Straße';
$originals = [ 'ä', 'ö', 'ü', 'Ä', Ö', 'Ü', 'ß'];
$replacements = ['ae', 'oe', 'ue', 'Ae', 'Oe', 'Ue', 'ss' ];
$cleaned = str_replace($originals, $replacements, $streetName);
//Gives us 'Schoenwald Strasse'
5
6. An introduction to strtr()
– Does ONE round of replacement, directly translating (hence the "tr") one
character or substring into another
– Has two possible input formats:
strstr(string $myString, string $from string $to)
or
strstr(string $myString, array $mappings)
– Not to be confused with strstr()
6
7. strtr() example one
– This example uses the simple format, specifying exactly one replacement pair.
$adjective = 'schön';
$cleaned = strtr($adjective, 'ö', 'oe');
//Gives us 'schoen'
7
8. strtr() example two
– The array format can work better for larger sets.
$streetName = 'Schönwald Straße';
$mappings = [ 'ä' => 'ae', 'ö' => 'oe', 'ü' => 'ue', 'Ä' => 'Ae' => 'Ö' => 'Oe', 'Ü' =>
'Ue', 'ß' => 'ss' ];
$cleaned = strtr($streetName, $mappings);
//Gives us 'Schoenwald Strasse'
8
10. Differences in behavior
… so these are basically the same.
… right?
No.
If given an array of mappings,
str_replace() replaces inside the
replacements, going from left to right,
leading to sometimes counterintuitive
results. So this code …
$output = [];
$myString = 'WordOne WordTwo WordThree
WordFour WordFive WordSix';
$mappings = [
'WordOne' => 'WordTwo',
'WordTwo' => 'WordThree',
'WordThree' => 'WordFour',
'WordFour' => 'WordFive',
'WordFive' => 'WordSix',
'WordSix' => 'WordSeven'
];
$output['str_replace_results'] = str_replace(
array_keys($mappings),
array_values($mappings),
$myString);
$output['strtr_results'] = strtr($myString, $mappings);
print_r($output);
10
12. Differences in behavior (continued)
There's more.
– The str_replace() function can take in a subject array, allowing replacement of
multiple (string-formatted) array values at once in different parts of the array
12
13. Differences in behavior (continued)
There's more.
– The str_replace() function can take in a subject array, allowing replacement of
multiple (string-formatted) array values at once in different parts of the array
– When you do this (or even use a simple string replacement), you can also give a
fourth argument: An int variable that will be modified by reference to hold the
number of replacements performed. So, for example …
13
15. Differences in behavior (continued)
You can even feed str_replace() a string for the subject and an array for the
replacement, allowing you to replace a series of identical wildcards in a document
with a sequence of ever-changing values.
(Similar to PDO parameter substitution -- but obviously use that safer, baked-in
PDO behavior instead if working with a database.)
15
16. Summary
– Use str_replace() with caution, as it may do multiple sequential replacements
– If that's a problem (and you don't need to count replacements), use strtr()
– If you need a count but you don't trust str_replace(), you can use strtr() twice
with a flag value
16
17. Thank you!
– These slides: https://www.slideshare.net/patrickmaynard3
– Ixopay careers page: https://www.ixopay.com/en/company/careers
– Social media links: https://patrickmaynard.com
17