Comparing two string modification functions

patrickmaynard3 16 views 17 slides May 13, 2024
Slide 1
Slide 1 of 17
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17

About This Presentation

A look at the differences between the str_replace() and strtr() functions, as presented at PHPDay 2024 in Verona, Italy.


Slide Content

Comparing two string modification functions Differences between behaviors of strtr() and str_replace() Patrick Maynard | Ixopay PHPDAY VERONA 2024 1

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

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

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

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

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

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

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

Differences in behavior … so these are basically the same. 9

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

Differences in behavior (continued) … yields this output: [str_replace_results] => WordSeven WordSeven WordSeven WordSeven WordSeven WordSeven [strtr_results] => WordTwo WordThree WordFour WordFive WordSix WordSeven 11

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

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

Differences in behavior (continued) $result = []; $count = 0; $inputStringsExample = ['EUR,USD','USD,EUR']; $abbreviations = ['EUR', 'USD']; $acceptedCurrencies = ['Euros', 'U.S. dollars']; $fixed = str_replace($abbreviations, $acceptedCurrencies, $inputStringsExample, $count); print_r($inputStringsExample); print $count; … yields this output: Array ( [0] => EUR,USD [1] => USD,EUR ) 4 14

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

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

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