...

string trouble

Pompiuses
08-02-2002, 12:21 PM
I have this string:

$old_text = "ert, ert,ert,ert,ert";

Sometimes there is a space behind the comma, sometimes it's not. How can I test if there is a space behind a comma, and if it is'nt put a space behind it?

So that $old_text prints:
ert, ert, ert, ert, ert

Something like this:

Search through the string $old_text, and test
if(there is space behind a comma)
{do nothing}
else
{insert a space behind the comma}

SYP}{ER
08-02-2002, 01:45 PM
$old_text = preg_replace ("/(, ?| ?,)/",", ",$old_text);

That should make it grammatically correct :D

mordred
08-02-2002, 01:46 PM
...I can't resist... must... use... Regular Expressions :)


$old_text = "ert, ert,ert,ert,ert";
$new_text = preg_replace('/,(?! )/', ', ', $old_text);
echo $new_text;


If you're curious, check out the preg_replace (http://za2.php.net/manual/en/function.preg-replace.php) function and the Pattern Syntax for lookahead assertions (http://za2.php.net/manual/en/pcre.pattern.syntax.php).

SYP}{ER
08-02-2002, 03:08 PM
Mines better :p

Mine'll turn this:

text, text , text ,text

Into this:

text, text, text, text

:)

mordred
08-02-2002, 10:18 PM
You're a genius. ;)

Mine does exactly what the original poster requested, nothing more or less, no hidden agendas as in your snippet. :p



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum