Page 1 of 1

Regex Expression Help Please

Posted: Sun Jun 18, 2006 5:36 am
by Benjamin
I have this text...
said" here are some words!" I
Here is my Regex

Code: Select all

$NewText = preg_replace('/[\s]*"[\s]*(.+)[\s]*"[\s]*/', " \"$1\" ", $NewText);
Here is the output, which is wrong because there are 2 spaces after the !.
said "here are some words! " I
And the output should be..
said "here are some words!" I

Posted: Sun Jun 18, 2006 6:20 am
by s.dot
After trying your regex (PHP 5.1.2) I get the following output:

Code: Select all

said "here are some words!" I
Looks good to me? No spaces after the !.

The only thing I can suggest is if you are replacing the text in the right variable
You have $NewText = preg_replace(.....,$NewText);
Which is perfectly legal, just seing if you meant to do that?

Posted: Sun Jun 18, 2006 6:56 am
by Benjamin
Hmm, yeah I am reusing the variable name on purpose.

It does work with () but not ""

I am using 5.0.4

Here is more code...

Code: Select all

$NewText = str_replace("\n  ", "\n", $NewText);  // removes 2 spaces after line feeds
  $NewText = preg_replace('/,(\w+)/', ", $1", $NewText);  // inserts a space after commas if there isn't one
  $NewText = preg_replace('/[\s]*"[\s]*(.+)[\s]*"[\s]*/', " \"$1\" ", $NewText);  // formats qouted text
  $NewText = preg_replace('/[\s]*\([\s]*(.+)[\s]*\)[\s]*/', " ($1) ", $NewText);  // formats parentheses
  $NewText = preg_replace('/\s*,/', ",$1", $NewText);  // removes whitespace before commas