dot-space in href regexp needed

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Patryk Konieczka

    dot-space in href regexp needed

    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_replac e(",",", ",$txt);
    $txt=str_replac e(".",". ",$txt);
    $txt=str_replac e(". ,",".,",$txt );
    $txt=str_replac e(". . . ","... ",$txt);
    $txt=str_replac e(":",": ",$txt);
    $txt=str_replac e(";","; ",$txt);
    $txt=str_replac e("]","] ",$txt);
    $txt=str_replac e("... ]","...] ",$txt);
    $txt=str_replac e(")",") ",$txt);
    $txt=str_replac e("... )","...) ",$txt);
    $txt=str_replac e(" "," ",$txt);
    $txt=str_replac e("„ ","„",$txt) ;
    $txt=str_replac e(". )",".) ",$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


  • Michael Fesser

    #2
    Re: dot-space in href regexp needed

    .oO(Patryk Konieczka)
    [color=blue]
    >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:[/color]

    Some things about this function:
    [color=blue]
    >function getCorrect($txt ) {
    > $txt=str_replac e(",",", ",$txt);
    > $txt=str_replac e(".",". ",$txt);
    >[...][/color]

    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.
    [color=blue]
    >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[/color]

    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($s earch, $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($tes t) 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

    Comment

    • Patrol

      #3
      Re: dot-space in href regexp needed

      Thank you very very much
      [color=blue]
      > HTH[/color]

      It sure did help, I really appreciate it
      Take care
      Patrol


      Comment

      Working...