473,249 Members | 1,618 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,249 software developers and data experts.

expression help needed

Hi there. I want to be able to add an extra space in each instance
after a period in a string. I am already stripping out extra
whitespace throughout, so to start I have a nice flat string to work
with. I am struggling getting the syntax right and keep getting funny
results. What is the simplest way to accomplish this? Thanks a
billion.

Matt

Jul 17 '05 #1
9 1805

"Matt MC" <ma******@yahoo.com> wrote in message
news:11*********************@l41g2000cwc.googlegro ups.com...
Hi there. I want to be able to add an extra space in each instance
after a period in a string. I am already stripping out extra
whitespace throughout, so to start I have a nice flat string to work
with. I am struggling getting the syntax right and keep getting funny
results. What is the simplest way to accomplish this? Thanks a
billion.

Matt


Please give code example.

Brent Palmer.
Jul 17 '05 #2
If I understand you correctly, I think this is what you want:

<?php
$str = trim(str_replace('.', '. ', $str));
?>

The trim removes any trailing spaces, if your string ends with a '.'.

Best Regards,

Peter

"Matt MC" <ma******@yahoo.com> wrote in message
news:11*********************@l41g2000cwc.googlegro ups.com...
Hi there. I want to be able to add an extra space in each instance
after a period in a string. I am already stripping out extra
whitespace throughout, so to start I have a nice flat string to work
with. I am struggling getting the syntax right and keep getting funny
results. What is the simplest way to accomplish this? Thanks a
billion.

Matt

Jul 17 '05 #3
Greetings. Here are a couple examples of what I am looking for:

This is the input sentence. Testing this out. Thank you.

should output to...

This is the input sentence. Testing this out. Thank you.

After thinking about this further, I want to do this with all major end
punctuations, so that would be ! ? and . Did I miss anything?

Sincerely,

Matt

Peter Albertsson wrote:
If I understand you correctly, I think this is what you want:

<?php
$str = trim(str_replace('.', '. ', $str));
?>

The trim removes any trailing spaces, if your string ends with a '.'.

Best Regards,

Peter

"Matt MC" <ma******@yahoo.com> wrote in message
news:11*********************@l41g2000cwc.googlegro ups.com...
Hi there. I want to be able to add an extra space in each instance
after a period in a string. I am already stripping out extra
whitespace throughout, so to start I have a nice flat string to work with. I am struggling getting the syntax right and keep getting funny results. What is the simplest way to accomplish this? Thanks a
billion.

Matt


Jul 17 '05 #4
I noticed that Message-ID:
<11**********************@o13g2000cwo.googlegroups .com> from Matt MC
contained the following:
This is the input sentence. Testing this out. Thank you.

should output to...

This is the input sentence. Testing this out. Thank you.

After thinking about this further, I want to do this with all major end
punctuations, so that would be ! ? and . Did I miss anything?


Is this for output to a browser?

Because browsers will ignore multiple spaces unless you make them non
breaking. Also you only want to do the replacement if the character is
already followed by a space. In this case, using trim will prevent the
replacement of the final full stop (period).

<?php

$punctuation=array(". ","! ","? ");
$punctuation_with_spaces=array(".&nbsp; ","!&nbsp; ","?&nbsp; ");
$test_string="\"Can you do it?\" he asked. I thought about it for a
moment. This would not be as simple as it first seemed. Difficult even!
Were all options covered? Hard to say. ";
$str = str_replace($punctuation, $punctuation_with_spaces,
$test_string);

print "<p>$test_string</p>";
print "<p>$str</p>";
?>
--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #5
Matt MC wrote:
Hi there. I want to be able to add an extra space in each instance
after a period in a string. I am already stripping out extra
whitespace throughout, so to start I have a nice flat string to work
with. I am struggling getting the syntax right and keep getting funny
results. What is the simplest way to accomplish this? Thanks a
billion.

Matt


This will make sure there are exactly 2 spaces after each of those marks:

$string = preg_replace("/([\.!?])(\s*)/", "\\1 ", $string);

-Joe
Jul 17 '05 #6
str_replace can take an array as the search terms. If any of the
search terms are found, they will be replaced. So:

$inputStr = "This is the input sentence. Testing this out. Thank you.";
$matches = array(". ", "? ", "! "); //one space after each one
$replacements = array(". ", "? ", "! "); //two spaces after each one
$outStr = str_replace($matches, $replacements, $inputStr);

Jul 17 '05 #7
Joe, I found if someone types multiple marks, like: testing!!! it will
output: testing! ! !

Can this be compensated for those scenarios?

Sincerely,

Matt

Joe Webster wrote:
Matt MC wrote:
Hi there. I want to be able to add an extra space in each instance
after a period in a string. I am already stripping out extra
whitespace throughout, so to start I have a nice flat string to work with. I am struggling getting the syntax right and keep getting funny results. What is the simplest way to accomplish this? Thanks a
billion.

Matt

This will make sure there are exactly 2 spaces after each of those

marks:
$string = preg_replace("/([\.!?])(\s*)/", "\\1 ", $string);

-Joe


Jul 17 '05 #8
Matt MC wrote:
Joe, I found if someone types multiple marks, like: testing!!! it will
output: testing! ! !

Can this be compensated for those scenarios?

Sincerely,

Matt

Joe Webster wrote:

Oh sure, just add a + after the [\.!?], so it should now read.

$string = preg_replace("/([\.!?]+)(\s*)/", "\\1 ", $string);

-Joe
Jul 17 '05 #9

Joe Webster wrote:
Matt MC wrote:
Joe, I found if someone types multiple marks, like: testing!!! it will output: testing! ! !

Can this be compensated for those scenarios?

Sincerely,

Matt

Joe Webster wrote:

Oh sure, just add a + after the [\.!?], so it should now read.

$string = preg_replace("/([\.!?]+)(\s*)/", "\\1 ", $string);

-Joe


Ahh, that works beautifully. Bravo!

---Matt

Jul 17 '05 #10

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

Similar topics

14
by: Tina Li | last post by:
Hello, I've been struggling with a regular expression for parsing XML files, which keeps giving the run time error "maximum recursion limit exceeded". Here is the pattern string: ...
23
by: Paul Rubin | last post by:
OK, I want to scan a file for lines matching a certain regexp. I'd like to use an assignment expression, like for line in file: if (g := re.match(pat, line)): croggle(g.group(1)) Since...
4
by: pekka niiranen | last post by:
Hi there, I have perl script that uses dynamically constructed regular in this way: ------perl code starts ---- $result ""; $key = AAA\?01; $key = quotemeta $key; $line = " ...
4
by: Neri | last post by:
Some document processing program I write has to deal with documents that have headers and footers that are unnecessary for the main processing part. Therefore, I'm using a regular expression to go...
13
by: Zeng | last post by:
Hello, Please help!!! I've been stuck on this issue for months. I just wonder if there is a way to programmatically evaluate expression strings such as ( ( 3 + 5 ) / 2 ) > 4 --> this...
2
by: Kjetil Klaussen | last post by:
Hi, I’m having some troubles trying to bind my dataset to a GridView control through an ObjectDataSource control. The binding works fine for regular columns in my dataset, but I can’t seem...
1
by: Jan Limpens | last post by:
Hello, I am creating a simple CRUD application which uses xml files as data storage. I extended Textbox with a XPath expression and a XmlDocument property, this way I can easily read and update...
9
by: Earl | last post by:
I have somewhat of an interesting scenario: The form allows the user to select a service, which populates a a grid of product information related to that service ("service grid"). The user can...
28
by: Marc Gravell | last post by:
In Linq, you can apparently get a meaningful body from and expression's .ToString(); random question - does anybody know if linq also includes a parser? It just seemed it might be a handy way to...
8
by: PJ6 | last post by:
Const factor As Double = Math.Sqrt(3) / 6 Error 1 Constant expression is required. This looks like laziness to me. In SQL Server, functions are given a distinction between ones that always...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...

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.