Library code snippets

Replace only the first occurance in a string

Although you can't do this with ereg_replace, you can use preg_replace instead:

<?
$var = 'abcdef abcdef abcdef';

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

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

Comments

  1. 01 Jan 1999 at 00:00

    This thread is for discussions of Replace only the first occurance in a string.

Leave a comment

Sign in or Join us (it's free).

 altphpfaq

Related podcasts

  • EarthClassMail.com - Moving from LAMP to .NET 3.5

    Scott chats with Matt Davis, architect at EarthClassMail.com, about their move from a LAMP stack (Linux/Apache/mysql/PHP) to .NET 3.5. What's working, what's not, and what kinds of issues are they running into as their architect their solution.

Events coming up

  • Apr 17

    WebTech Conference 2010 - Bulgaria

    Veliko Turnovo, Bulgaria

    6th edition of WebTech conference will be held. A 2 day conference about : - Web Technologies - Blogs and blogging - Web 3.0 - Open Web - Mobile technologies - Internet Business

We'd love to hear what you think! Submit ideas or give us feedback