By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,714 Members | 1,299 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,714 IT Pros & Developers. It's quick & easy.

dot-space in href regexp needed

P: n/a
Hello
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:

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

TIA
Patrol
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
.oO(Patryk Konieczka)
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:
Some things about this function:
function getCorrect($txt) {
$txt=str_replace(",",", ",$txt);
$txt=str_replace(".",". ",$txt);
[...]
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.
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


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
way.
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{4});|[.,:)\]])(?![\s.,:;)\]]))/e';
$replace = '"$1" == "$2" ? "$1" : "$1 "';
return preg_replace($search, $replace, $txt);
}

A test string:

$test =
'Foo,bar:This here...is (just)a simple
<a href="http://www.example.com">Test</a>.„Maybe it
works (as expected),maybe not...';

getCorrect($test) returns:

'Foo, bar: This here... is (just) a simple
<a href="http://www.example.com">Test</a>. „Maybe it
works (as expected), maybe not... '

HTH
Micha
Jul 17 '05 #2

P: n/a
Thank you very very much
HTH


It sure did help, I really appreciate it
Take care
Patrol
Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.