How To Insert A String Into Another String In PHP

Karol Krizka @ December 29, 2007

Programming

Most programming languages have a function that allows you to insert one string into another string at a given position. For example, I can take the string blue and the string HI, and perform an operation blue.insert(HI,2) to get the resulatant string blHIue. But such a function does not come with the standard PHP library, at least not obviously. There is no function str_insert and a simple Google search does not bring any results. So one has to get creative and use the substr_replace function to insert a string into another by replacing a sub string of length 0 at the wanted position in the original.
The following is the arguments you should use to the substr_replace function if you want to insert one string into another string in PHP:
$newstring=substr_replace($orig_string, $insert_string, $position, 0);
Quite simple, no?

Did you enjoy this post? Then why not subscribe to my RSS feed or subscribe by e-mail? Also check out the many other FREE ways to appreciate a blogger.


Related Posts

RSS feed | Trackback URI

Comments »

No comments yet.

Name (required)
E-mail (required - never shown publicly)
URI
Subscribe to comments via email
Your Comment (smaller size | larger size)
You may use <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line=""> in your comment.

Feed