Automate paragraphs in a post page to have a unique anchor link

I think that should really be <a name="p1"> around the paragraphs with link to the paragraphs being <a href="#p1">.

If that is what you are trying to accomplish ( what used to be called a named anchors ), that technique is not valid in HTML 5. You can instead link to any id in the page, so with that in mind.

$count = 0;
function auto_id_p_cb($matches) {
  var_dump($matches);
  global $count;
  $count++;
  $ret="<p id="p_".$count.'"';
  if (!empty($matches[1])) $ret .= $matches[1];
  $ret .= '>';
  return $ret;
}
function auto_id_p($content){
  $content = preg_replace_callback('/<p([^>]*)>/','auto_id_p_cb',$content);
  return $content;
}
add_filter('the_content','auto_id_p',1000);

Minimally tested but it does preserve existing attributes in the <p> tags. I am sure it would produce a duplicate id if one already existed in the <p> so watch that. Think of it more as “proof of concept”.

If you do still what to do it with actual <a> tags, you should be able to adapt that.