0

I have this code in my functions.php file:

function user_content_replace($content) {

    // it's not a URL, let's apply the replacement
    if (!filter_var($string, FILTER_VALIDATE_URL)) {
        $replacement = '$1.</p><p>$2';
        return preg_replace("/([^\\.]*\\.[^\\.]*\\.[^\\.]*){1}\\.([^\\.]*)/s", $replacement, $content);
    } else { // it's a URL, just return the string
        return $content;
    }
}
add_filter('the_content','user_content_replace', 99);

This code replaces every third dot in content with dot+closed+open paragraph. This because it is the best way in this moment to format non-formatted great amount of texts and posts.

But: this code also changes Images URLs so all my images does not contain dot before extension but **imagename.</p><p>jpg**instead of imagename.jpg

Even if I put URL validation - same problem. Any advice please?

  • I don't get what you are trying to do. Why include a paragraph every third dot? Also, WordPress already applies wpautop() to content, why it doesn't work for you?. By the way, you have a typo: filter_var($string should be filter_var($content. – cybmeta May 7 '16 at 8:01
  • OK, it is not typo (works same with string or content variable) and reason for this is a lot of non.formatted imported posts, so this is one way to create line breaks after every three sentences. But I dont know why this code changes images URLs? the_content does not includes image paths...so I dont get it – Eager2Learn May 7 '16 at 16:34
  • As it sounded before, you do filter nothing with filter_var() function. Secondly, it's most likely a problem with your preg_replace() pattern. I don't understand the double escapes `\\` there. – Max Yudin May 8 '16 at 17:38
  • @Eager2Learn, note that $string is an undefined variable in your code. If it is not a typo it is bug. Note also that I'm not saying that it is the solution to your problem. – cybmeta May 8 '16 at 18:35
0
<?php
$string = 'Sentence 1. Sentence 2? Sentence 3! Sentence 4... Sentence 5… Sentence 6! Sentence 7. Sentence 8. Sentence 9… Sentence 10... Sentence 11. ';

$sentences_per_paragraph = 3; // settings

$pattern = '~(?<=[.?!…])\s+~'; // some punctuation and trailing space(s)

$sentences_array = preg_split($pattern, $string, -1, PREG_SPLIT_NO_EMPTY); // get sentences into array

$sentences_count = count($sentences_array); // count sentences

$output = ''; // new content init

// see PHP modulus
for($i = 0; $i < $sentences_count; $i++) {
    if($i%$sentences_per_paragraph == 0 && $i == 0) { //divisible by settings and is first
        $output .= "<p>" . $sentences_array[$i] . ' '; // add paragraph and the first sentence
    } elseif($i%$sentences_per_paragraph == 0 && $i > 0) { //divisible by settings and not first
        $output .= "</p><p>" . $sentences_array[$i] . ' '; // close and open paragraph, add the first sentence
    } else {
        $output .= $sentences_array[$i] . ' '; // concatenate other sentences
    }
}

$output .= "</p>"; // close the last paragraph

echo $output;

Note: It's a very rough code that does not check the deeper problems.

For more info:

  • Beautiful, thank you mate! I did not work with this function before! – Eager2Learn May 8 '16 at 23:39
1

Thanks to @Max Yudin this is an answer to my problem:

function user_content_replace($content) {

$sentences_per_paragraph = 3; // settings

$pattern = '~(?<=[.?!…])\s+~'; // some punctuation and trailing space(s)

$sentences_array = preg_split($pattern, $content, -1, PREG_SPLIT_NO_EMPTY); // get sentences into array

$sentences_count = count($sentences_array); // count sentences

$output = ''; // new content init

// see PHP modulus
for($i = 0; $i < $sentences_count; $i++) {
    if($i%$sentences_per_paragraph == 0 && $i == 0) { //divisible by settings and is first
        $output .= "<p>" . $sentences_array[$i] . ' '; // add paragraph and the first sentence
    } elseif($i%$sentences_per_paragraph == 0 && $i > 0) { //divisible by settings and not first
        $output .= "</p><p>" . $sentences_array[$i] . ' '; // close and open paragraph, add the first sentence
    } else {
        $output .= $sentences_array[$i] . ' '; // concatenate other sentences
    }
}

$output .= "</p>"; // close the last paragraph

echo $output;
}
add_filter('the_content','user_content_replace', 99);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.