Eager2Learn Eager2Learn - 1 year ago 85
PHP Question

Use function preg_replace to replace period with paragraph

I have a lot of unformatted text in my WP posts; i tried to use this code:

function user_content_replace($content) {
return str_replace('.','.</p><p>',$content);
add_filter('the_content','user_content_replace', 99);

So in fact i want to replace PERIOD with PARAGRAPH and PERIOD (

But I need a little bit help here: first - this code replaces EVERY DOT, so all my permalinks, image paths, etc. are substituted with DOT and that is bad, web does not work at all in this case.

So, i think that solution to replace EVERY THIRD DOT will be salvation for me, so not every DOT.

Any ideas?


Answer Source

I need to replace EVERY THIRD DOT, that is all

Code sample:

$content = "first.sec\nond.third.fourth.fifth>.sixth].seventh.";
$replacement = '$1.</p><p>$2';
echo preg_replace("/([^\\.]*\\.[^\\.]*\\.[^\\.]*){1}\\.([^\\.]*)/s", $replacement, $content);
echo "\n";

Output of the above code:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download