Saturday, December 14, 2013

Find and Replace with Regular Expressions in PHP

If we want to remove a specific part from a string, we can use regular expressions in php

<?php $oldstring="12. What is Your Name";
$newstring=preg_replace('/[\d]+[\.]+[\s?]/', '',$oldstring);
echo $newstring;

Result : What is Your Name
Here removes serial number from a string. eg : 12. Pattern match search for [\d]+[\.] . This means any digit followed by a dot(.)