473,395 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to insert a string into another string?

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?

Thanks is advance
Ken Robinson
kenrbnsn (at) rbnsn (dot) com
Jul 17 '05 #1
3 7784
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!
Jul 17 '05 #2
Senator Jay Billington Bulworth <fu***@fung.arg> writes:

<?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.

Right. It's the "word voundary" spec that you're looking for...

<?;

print preg_replace('/\b(\S)/',
'$1XXX',
'this is a test string'
);

?>


--
-------------------------------------------------------------------------------
Jerry Sievers 305 854-3001 (home) WWW ECommerce Consultant
305 321-1144 (mobile http://www.JerrySievers.com/
Jul 17 '05 #3

Uzytkownik "Jerry Sievers" <je***@jerrysievers.com> napisal w wiadomosci
news:m3************@prod01.jerrysievers.com...
Right. It's the "word voundary" spec that you're looking for...

<?;

print preg_replace('/\b(\S)/',
'$1XXX',
'this is a test string'
);

?>


Look, mom! No capturing!

print preg_replace('/(?<=\b\S)/',
'XXX',
'this is a test string'
);
Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: jason | last post by:
How does one loop through the contents of a form complicated by dynamic construction of checkboxes which are assigned a 'model' and 'listingID' to the NAME field on the fly in this syntax:...
3
by: Howard Hinnant | last post by:
I recently asked for a survey of multimap insert with hint behavior, in support of a paper I'm writing concerning lwg issue 233. My sincere thanks to Beman Dawes, Raoul Gough, Russell Hind, Bronek...
7
by: tano | last post by:
Hello, I have to insert a char in the middle of a string, I have written two functions but I don't know what is the better? The problem is: if I use malloc() I copy all the string with the new...
13
by: Matt | last post by:
10 len space designated strings grows when i do sb.insert. is there way to stop string growing. It becomes 14 space len on test.file I like to be able insert 3rd position but length should stay...
2
by: Tim::.. | last post by:
Can someone please tell me why I keep getting the following error from the code below! Error: INSERT statement conflicted with COLUMN FOREIGN KEY constraint...
20
by: Guadala Harry | last post by:
In an ASCX, I have a Literal control into which I inject a at runtime. litInjectedContent.Text = dataClass.GetHTMLSnippetFromDB(someID); This works great as long as the contains just...
6
by: SandySears | last post by:
I am trying to use a stored procedure to insert a record using VS 2005, VB and SQL Server Express. The code runs without errors or exceptions, and returns the new identifer in the output...
6
by: rn5a | last post by:
During registration, users are supposed to enter the following details: First Name, Last Name, EMail, UserName, Password, Confirm Password, Address, City, State, Country, Zip & Phone Number. I am...
4
by: Bob | last post by:
Hi all, I'm trying to import data, modify the data then insert it into a new table. The code below works fine for it but it takes a really long time for 15,000 odd records. Is there a way I...
0
by: troydixon | last post by:
Hello, I am new at this, and have been trying to insert data into a table by using the footer of a gridview (which I dont like) or by using a detials view on the same page that is doing the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.