MultiStringReplacer.php
97 lines
| 1 | <?php |
| 2 | /** |
| 3 | * AhoCorasick PHP Library |
| 4 | * |
| 5 | * A PHP implementation of the Aho-Corasick string matching algorithm. |
| 6 | * |
| 7 | * Alfred V. Aho and Margaret J. Corasick, "Efficient string matching: |
| 8 | * an aid to bibliographic search", CACM, 18(6):333-340, June 1975. |
| 9 | * |
| 10 | * @link http://xlinux.nist.gov/dads//HTML/ahoCorasick.html |
| 11 | * @link https://en.wikipedia.org/wiki/Aho-Corasick_string_matching_algorithm |
| 12 | * |
| 13 | * Copyright (C) 2015 Ori Livneh <ori@wikimedia.org> |
| 14 | * |
| 15 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 16 | * you may not use this file except in compliance with the License. |
| 17 | * You may obtain a copy of the License at |
| 18 | * |
| 19 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 20 | * |
| 21 | * Unless required by applicable law or agreed to in writing, software |
| 22 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 23 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 24 | * See the License for the specific language governing permissions and |
| 25 | * limitations under the License. |
| 26 | * |
| 27 | * @file |
| 28 | * @author Ori Livneh <ori@wikimedia.org> |
| 29 | */ |
| 30 | |
| 31 | namespace AhoCorasick; |
| 32 | |
| 33 | /** |
| 34 | * This class extends MultiStringMatcher, adding search and replace |
| 35 | * functionality. |
| 36 | */ |
| 37 | class MultiStringReplacer extends MultiStringMatcher { |
| 38 | |
| 39 | /** @var array Mapping of states to outputs. **/ |
| 40 | protected $replacePairs = []; |
| 41 | |
| 42 | /** |
| 43 | * Constructor. |
| 44 | * |
| 45 | * @param array $replacePairs array of ( 'from' => 'to' ) replacement pairs. |
| 46 | */ |
| 47 | public function __construct( array $replacePairs ) { |
| 48 | foreach ( $replacePairs as $keyword => $replacement ) { |
| 49 | if ( $keyword !== '' ) { |
| 50 | $this->replacePairs[$keyword] = $replacement; |
| 51 | } |
| 52 | } |
| 53 | parent::__construct( array_keys( $this->replacePairs ) ); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Search and replace a set of keywords in some text. |
| 58 | * |
| 59 | * @param string $text The string to search in. |
| 60 | * @return string The input text with replacements. |
| 61 | * |
| 62 | * @par Example: |
| 63 | * @code |
| 64 | * $replacer = new MultiStringReplacer( array( 'csh' => 'sea shells' ) ); |
| 65 | * $replacer->searchAndReplace( 'She sells csh by the sea shore.' ); |
| 66 | * // result: 'She sells sea shells by the sea shore.' |
| 67 | * @endcode |
| 68 | */ |
| 69 | public function searchAndReplace( $text ) { |
| 70 | $state = 0; |
| 71 | $length = strlen( $text ); |
| 72 | $matches = []; |
| 73 | for ( $i = 0; $i < $length; $i++ ) { |
| 74 | $ch = $text[$i]; |
| 75 | $state = $this->nextState( $state, $ch ); |
| 76 | foreach ( $this->outputs[$state] as $match ) { |
| 77 | $offset = $i - $this->searchKeywords[$match] + 1; |
| 78 | $matches[$offset] = $match; |
| 79 | } |
| 80 | } |
| 81 | ksort( $matches ); |
| 82 | |
| 83 | $buf = ''; |
| 84 | $lastInsert = 0; |
| 85 | foreach ( $matches as $offset => $match ) { |
| 86 | if ( $offset >= $lastInsert ) { |
| 87 | $buf .= substr( $text, $lastInsert, $offset - $lastInsert ); |
| 88 | $buf .= $this->replacePairs[$match]; |
| 89 | $lastInsert = $offset + $this->searchKeywords[$match]; |
| 90 | } |
| 91 | } |
| 92 | $buf .= substr( $text, $lastInsert ); |
| 93 | |
| 94 | return $buf; |
| 95 | } |
| 96 | } |
| 97 |