• On a project in progress, I have to add aspaces after punctuation.(and before sometime)
    It is a blog with more than 37000 posts. And the blogger has long drafted forgetting to add for example a space after:
    –> ‘.’
    –> ‘;’
    –> ‘,’
    –> ‘?’
    and a space before and after:
    –> ‘()’
    –> ‘:’

    This is the code, thank you pepe (wp-typography) Based on the regex :

    add_filter( 'the_content', 'spacify' );
    function spacify( $text ) {
    $text = preg_replace( '@(\w+)(\.|;|,|!|\?|\))(\S)@u', '$1$2 $3', $text );
    $text = preg_replace( '@(\w+)(:)(\S)@u', '$1 $2 $3', $text );
    return $text;
    }

    [Moderator note: code fixed. Please wrap code in the backtick character or use the code button.]

    this code is ok but breaking the links in the text and missing space before ‘(‘
    Anyone with an idea?
    There are no existing plugins for this and the best way is this.
    The best solution is to call the community for it, I’m sure that I am not the only one to have this type of problem…

    • This topic was modified 7 years, 10 months ago by lolo343.
    • This topic was modified 7 years, 10 months ago by bdbrown.
Viewing 9 replies - 1 through 9 (of 9 total)
  • You didn’t specify ( in your original post, so it is not included in the regex. The regex was intended as “proof of concept” to nudge you in the right direction (although I admit I didn’t think the suggestion through: If your content contains HTML elements that should be preserved, replacing punctuation via regexs becomes a hard problem).

    If you really need to do it, parse the content into a DOM and iterate over the text nodes with a similar set of regular expressions.

    • This reply was modified 7 years, 10 months ago by pepe.
    • This reply was modified 7 years, 10 months ago by pepe.
    Thread Starter lolo343

    (@lolo343)

    add_filter( 'the_content', 'spacify' );
    function spacify( $text ) {
    $text = preg_replace( '@(\w+)(;|,|\.|!|\?|\))(\S)@u', '$1$2 $3', $text );
    $text = preg_replace( '@(\w+)([(])(\S)@u', '$1 $2$3', $text );
    $text = preg_replace( '@(\w+)(:)(\S)@u', '$1 $2 $3', $text );
    return $text;
    }

    with a (

    and after to ignore the links :

    add_filter( 'the_content', 'spacify_remove_spaces_double_dots_http' );
    function spacify_remove_spaces_double_dots_http( $text ) {
    $text = str_replace( 'http : ', 'http:', $text );
    return $text;
    }

    and for exemple to ignore de .com :

    add_filter( 'the_content', 'spacify_remove_spaces_com' );
    function spacify_remove_spaces_com( $text ) {
    $text = str_replace( '. com', '.com', $text );
    return $text;
    }
    Thread Starter lolo343

    (@lolo343)

    anybody has an idea to ignore the dot for exemple :

    200.000 $
    with the code it’s actually
    200. 000$

    Thread Starter lolo343

    (@lolo343)

    not for the currency just for removing the space after the .

    Have you thought about just doing the replacement directly in the post_content field in the database?

    That way you don’t have to filter the content on every page view.

    Thread Starter lolo343

    (@lolo343)

    @ancawonka
    post_content have links …. no?

    Thread Starter lolo343

    (@lolo343)

    The method will be carried out on about 37 000 post

    If you can figure out a series of replace queries to run on the database, updating 37,000 posts is not a big deal.

    You’ll have to figure out a sequence of replacements to do (e.g. so that you don’t destroy your links or prices) but once you do it you will be done.

    Try it on a duplicate copy of the database or a development environment first.

    In the long run, fixing the content in the database is probably the best idea (though I’m wondering who wrote that in the first place).

    I might add a feature to run custom replacement filters to a future version of wp-Typography, but that probably won’t help for the timeframe of this project.

    • This reply was modified 7 years, 10 months ago by pepe.
Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Adding spaces after punctuation’ is closed to new replies.