Created
June 20, 2018 19:20
-
-
Save Phoenix2k/003a30d7f648a0cfec7c30825dab1003 to your computer and use it in GitHub Desktop.
PHP: Search and replace function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Search and replaces text. | |
* | |
* @param String $text Text to be replaced | |
* @param Array $args Key value pairs representing search and replace respectively | |
* @return String Text with search and replace applied. | |
*/ | |
function search_and_replace( $text = '', $args = [ ] ) { | |
if ( empty( $args ) || ! array( $args ) ) return $text; | |
foreach ( $args as $search => $replace ) { | |
if ( false !== strpos( $text, $search ) ) { | |
$text = str_replace( $search, $replace, $text ); | |
} | |
} | |
return $text; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment