dot-space in href regexp needed

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
Here's the thing
I have a database edited by some company workers editing descriptions of
books in the sotre , unfortunately these workers do not have the habit of
inserting a space character after word-ending dot, this is why i use the

function getCorrect($txt) {
 $txt=str_replace(",",", ",$txt);
 $txt=str_replace(".",". ",$txt);
 $txt=str_replace(". ,",".,",$txt);
 $txt=str_replace(". . . ","... ",$txt);
 $txt=str_replace(":",": ",$txt);
 $txt=str_replace(";","; ",$txt);
 $txt=str_replace("]","] ",$txt);
 $txt=str_replace("... ]","...] ",$txt);
 $txt=str_replace(")",") ",$txt);
 $txt=str_replace("... )","...) ",$txt);
 $txt=str_replace("  "," ",$txt);
 $txt=str_replace("„ ","„",$txt);
 $txt=str_replace(". )",".) ",$txt);
return $txt;

this basically closes the problem, but what bothers me recently is that i
need to indert a href link into this description and i get a result like
www. blah. com/index. php

is there anone who could help me out writing a regexp converting a ". " into
"." if it is inside href="xxx"
i'm not very familiar with regexp's so i would really appreciate your help


Re: dot-space in href regexp needed

 .oO(Patryk Konieczka)

Quoted text here. Click to load it

Some things about this function:

Quoted text here. Click to load it

1) It's not the best way to call functions like str_replace() again and
again, there are ways to do such replacements with a single function
call (with strtr() for example).

2) You don't need double quotes there, single quotes around the strings
are enough. This saves some CPU cycles and some work for the parser,
because there are no embedded variables it has to look for.

3) Especially in such constructs like above it makes sense to separate
the function arguments not only by comma, but by a comma and a space,
which makes the code a bit more readable.

Quoted text here. Click to load it

OK, I've done the entire thing with a regular expression - and it looks
really terrible ... ;)

It should do nearly the same like your original function. There are some
little modifications on when a space will be added to punctuation chars
and when not. I tried to avoid single tests for each special char and
used a more general approach. You have to test if it works for you this
Additionally it replaces chars only outside of HTML tags. Such "negative
matching" is not possible with regular expressions (except for negative
character classes and assertions, but this won't work here) and has to
be done with PHP. Thankfully PHP knows the modifier e, so it's possible
to "smuggle" some executable code into the pattern matching process.

The function:

function getCorrect($txt) {
  $search = '/((<[^>]*)|((?<!&#\d);|[.,:)\]])(?![\s.,:;)\]]))/e';
  $replace = '"$1" == "$2" ? "$1" : "$1 "';
  return preg_replace($search, $replace, $txt);

A test string:

$test =
'Foo,bar:This (just)a simple
<a href=" ">Test</a>.&#8222;Maybe it
works (as expected),maybe not...';

getCorrect($test) returns:

'Foo, bar: This here... is (just) a simple
<a href=" ">Test</a>. &#8222;Maybe it
works (as expected), maybe not... '


Re: dot-space in href regexp needed

Thank you very very much

Quoted text here. Click to load it

It sure did help, I really appreciate it
Take care

Site Timeline