473,405 Members | 2,310 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,405 software developers and data experts.

preg_split


lets say I have a line of text with multiple spaces between each word..
I am not sure how many spaces. I would like to take the line of text,
and change it to put only one space between the words.

So, my idea was to split it on the spaces, then implode it with one
space between each word.

But, this doesn't work:

preg_split("/\\s*/","test test test test")

I would think that the regular expression would match multiple spaces,
and split on that, but instead it just breaks apart each character of
the string.

How can I break it apart so I get something like:

Array (
[0] => "test",
[1] => "test",
[2] => "test"
)

Oct 3 '05 #1
2 3173
www.douglassdavis.com wrote:
lets say I have a line of text with multiple spaces between each word..
I am not sure how many spaces. I would like to take the line of text,
and change it to put only one space between the words.
If you just want to replace multiple blanks between words try:
$string = preg_replace("/\s+/", " ", $string);
So, my idea was to split it on the spaces, then implode it with one
space between each word.

But, this doesn't work:

preg_split("/\\s*/","test test test test")
Replace * with +
I would think that the regular expression would match multiple spaces,
and split on that, but instead it just breaks apart each character of
the string.

How can I break it apart so I get something like:

Array (
[0] => "test",
[1] => "test",
[2] => "test"
)


$array = preg_split("/\s+/",$string);

--
Andrew @ Rockface
np: (Winamp is not active ;-)
www.rockface-records.co.uk
Oct 3 '05 #2

Andrew @ Rockface wrote:
www.douglassdavis.com wrote:
lets say I have a line of text with multiple spaces between each word..
I am not sure how many spaces. I would like to take the line of text,
and change it to put only one space between the words.


If you just want to replace multiple blanks between words try:
$string = preg_replace("/\s+/", " ", $string);


much better... thanks.
So, my idea was to split it on the spaces, then implode it with one
space between each word.

But, this doesn't work:

preg_split("/\\s*/","test test test test")


Replace * with +


I hadn't thought of that... makes sense.

Oct 3 '05 #3

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

Similar topics

2
by: Peter | last post by:
L.S. I am trying to cut a string into substrings. The string at hand looks something like this: $data='one|two|three|four\nfive|six|seven|eight'; //observe the chr(10) in the middle I...
16
by: Sims | last post by:
Hi, I need some help to split data using regular expression Consider the string '1,2,3', I can split it using, preg_split("/,/", '1,2,3') and i correctly get =1, =2,=3. Now if i have
15
by: voipcanada | last post by:
is there any thing we can write to keep the right txt to the = sign in the preg_split. from the following lines DST-NUMBER-IN=0033512877596, DST-NUMBER-OUT=98751#33512877596,...
3
by: none | last post by:
Hello: Using PHP 4.3.11 The line: $strDate = preg_split('//', $my_date); works to break apart a date in the form mm/dd/yyyy or mm.dd.yyyy or mm-dd-yyyy but the line: $strDate =...
0
by: tiggman | last post by:
Why preg_split works only if the char # before the delimiter is 21 or less? example php code: ============= $right = "{Hello Im very aaaaaa {cool|not cool}}"; print $right."<br>"; $pattern =...
1
by: skippychalmers | last post by:
Hey... new here. Could really use some help with a preg_split i'm trying to run. Basically, I have a string. In it is the tag: ''. Its a sort of bbcode feature I'm adding to a forum. There's...
3
by: mark1491 | last post by:
I am trying to split a string into sentences with preg_split, but I would like it to not split initials. Examples: Mark H. Doolittle is my name. What is yours. ( I don't want it to split the...
14
by: pascalito | last post by:
Hi, i have a problem with a regular expression. i want to split a string on character pipe | with preg_split php function. The string is : "foo|bar|'hello|world\'abc\'def'|bye"
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.