Browsing:

Tag: ereg_replace

PHP replace only first occurrence of a string match

How to replace only the first occurrence of a string in php. Say you wanted to only replace the first occurrence of a string match instead of all occurrences You would use php preg_replace function to do the trick. Example is below: <? $data = ‘abcdef abcdef abcdef’; // pattern, replacement, string, limit echo preg_replace(‘/abcd/’, ‘webtechsource’, $data, 1); // outputs ‘webtechsourcedef abcdef abcdef’ ?> Just incase if you wanted to Read more…