我如何检查前几个字符,如果在PHP中第一次匹配x,那么在最初几次之后添加字符?

I want to perform a three step process:

  1. Check the first few characters (ffc) of a string variable.
  2. If ffc = x (another string of characters) Then
  3. Insert x after ffc but before any other content in ffc.

How can I accomplish this in PHP?

Actual Use Case:

I am using WordPress and grabbing the_content() and moving it into a variable $content.

I want some characters to appear before the text in $content, but WP auto adds <p> tags (if wpautop is on, and I'd like to avoid turning it off), which means the characters I add appear above rather than on the same line as $content.

The goal here is to check if $content starts with <p> and if it does to insert after <p> the characters "Summary: ".

Here is what I have thus far (it isn't working):

<?php
 $content = get_the_content();
 echo $content;
 $hasP = substr($content, 0, 3);
 echo $hasP; 
 If ($hasP == '<p>') {
   echo "Yes!";
   $newString = substr($string, 3);
   echo $newString;
 };
 ?>

Unfortunately, it seems that WP just re-adds the <p> when I echo $newString.

So, this took a long time for me to figure out, but here is the solution I came up with (err, make that stole from other people):

<?php
add_filter('the_content', 'before_after');
$content = the_content();
echo $content;
?>

And then the actual magic happens here:

function before_after($content) {
    $content = preg_replace('/<p>/', '<span>', $content, 1);
    $content = preg_replace('/<\/p>/', '</span>', $content, 1);
    return $content;
}

In the above I actually went beyond what I had initially stated and replaced both the opening and closing <p>.