Powered by InterCreatives | Site Map
Search:
NewSourceMedia Popular Tutorials: PHP Javascripts Web Hosting
SoftwareTutorials
CATEGORIES
Job Listings
 
Home / PHP / Content Management

PHP replace only first occurrence of a string match.


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.

The code is below:

<?
$var = 'abcdef abcdef abcdef';

// pattern, replacement, string, limit
echo preg_replace('/abc/', '123', $var, 1); // outputs '123def abcdef abcdef'

?>

Just incase you wanted to still match all occurrences, just use the php function erag_replace.
The code is below:


<?
$var = 'abcdef abcdef abcdef';

// pattern, replacement, string
echo ereg_replace('abc', '123', $var); // outputs '123def 123def 123def'

?>


Next PHP Page:
1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  [27]  28  29  30  31  32 





Hosting by iPowerWeb.com - 50 Gb for $7.95