473,385 Members | 1,465 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,385 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 1811

"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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?

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.