3v4l.org

run code in 300+ PHP versions simultaneously
<?php $indicator = '<span class="required">*</span>'; echo "** Before **\n"; var_dump( $indicator ); function replace_glyth( $haystack, $replacement ) { return str_replace( '*', $replacement, $haystack ); } // Replacing the asterisk. echo "\n\n** Direct replacement **\n"; var_dump( replace_glyth( $indicator, 'required' ) ); function replace_translatable_glyth( $haystack, $replacement ) { $pattern = '/(<span class="required">)(.*)(<\/span>)/i'; $replacement = sprintf( '${1}%s${3}', $replacement ); $new = preg_replace( $pattern, $replacement, $haystack ); return $new; $pattern = '/<span class="required">(?<glyth>.*)<\/span>/i'; preg_match( $pattern, $haystack, $matches ); var_dump( $matches ); // No glyth found. if ( empty( $matches['glyth' ] ) ) { return $haystack; } return str_replace( $matches['glyth'], $replacement, $haystack ); } // Since the asterisk is translatable ... echo "\n\n** Pattern to adjust for translatable glyth **\n"; var_dump( replace_translatable_glyth( $indicator, '**required**' ) );

preferences:
23.27 ms | 408 KiB | 5 Q