In article <2d******************************@news.teranews.co m>,
Ken Robinson <se**********@rbnsn.com> wrote:
This one has me stumped...
Let's say I have a string consisting of n words (where n > 0). I would like
to insert string 'abcd' between the first and second characters of each
word of the string.
I could probably use a combination of explode, implode and substr, but I
was wondering if there is a more elegant way of doing it using regular
expressions?
Try this,
<?php
$string = "This is a test string.";
$string = preg_replace("/( \w)(\w+)/", "\\1abcd\\2", $string);
$string = preg_replace("/^(\w)(\w+)/", "\\1abcd\\2", $string);
echo $string;
?>
Output: Tabcdhis iabcds a tabcdest sabcdtring.
There's probably a regexp to match the first word in the string _and_
words preceded by a space at the same time, and do it all in one line.
hth
--
Bulworth :
fu***@fung.arg | My email address is ROT13 encoded, decode to mail
--------------------------|--------------------------------------------------
<http://www.phplabs.com/> | PHP scripts and thousands of webmaster resources!