<?php $str = 'text abcd text text efgh';echo preg_replace('~(.*)text~su', '${1}bar', $str);
Standard input is empty
text abcd text bar efgh
The brand new service which powers Ideone!
Widget for compiling and running the source code in a web browser!