How to use a variable as a className inside preg_match_all parameters?

Note: this question is not related to WordPress, but rather to PHP directly and should be asked at StackOverflow but I have provided a very basic answer below to help you on your way. However be aware that this answer is not necessarily the best way to go about solving your problem, nor is it meant to be efficient.

// assuming get_the_content() equates to ↓

$content="<p class="1">P1 Content</p><p class="2">P2 Content</p><p class="3">P3 Content</p>";

$classNames = ['1', '2', '3'];

$matches = [];

foreach( $classNames as $className ) {

    $pattern = sprintf('\<p\sclass=\"%s\"\>(.*?)\<\/p\>', $className);

    preg_match("/{$pattern}/i", $content, $found); 

    $matches[] = $found;

}

var_dump($matches);

/*
array(3) {
  [0]=>
  array(2) {
    [0]=>
    string(27) "<p class="1">P1 Content</p>"
    [1]=>
    string(10) "P1 Content"
  }
  [1]=>
  array(2) {
    [0]=>
    string(27) "<p class="2">P2 Content</p>"
    [1]=>
    string(10) "P2 Content"
  }
  [2]=>
  array(2) {
    [0]=>
    string(27) "<p class="3">P3 Content</p>"
    [1]=>
    string(10) "P3 Content"
  }
}
*/

UPDATE 1:

If you do not have a fixed, known-list (ahead of time) of class names, then use the following example which utilises preg_match_all and looks for any <P> tag with class = NUMBER.

<p class="1">Content</p>
<p class="2">Content</p>
<!-- etc.. -->
<p class="234">Content</p>
$content="<p class="1">P1 Content</p><p class="2">P2 Content</p><p class="3">P3 Content</p>";

$pattern = sprintf('\<p\sclass=\"%s\"\>(.*?)\<\/p\>', '[0-9]+');

var_dump($pattern);

preg_match_all("/{$pattern}/i", $content, $found);

var_dump($found);

/*
array(2) {
  [0]=>
  array(3) {
    [0]=>
    string(27) "<p class="1">P1 Content</p>"
    [1]=>
    string(27) "<p class="2">P2 Content</p>"
    [2]=>
    string(27) "<p class="3">P3 Content</p>"
  }
  [1]=>
  array(3) {
    [0]=>
    string(10) "P1 Content"
    [1]=>
    string(10) "P2 Content"
    [2]=>
    string(10) "P3 Content"
  }
}
*/
/**
 * Iterate over the results (if any) and do something with the content.
 */
if ( is_array($found) && isset($found[1]) ) {

    foreach( $found[1] as $content ) {

        echo $content;

    }

}

Results in:

P1 Content
P2 Content
P3 Content

I advise that you prefix your class names such as:

<p class="myprefix-1">Content</p>
<p class="myprefix-2">Content</p>
<!-- etc.. -->
<p class="myprefix-234">Content</p>

If so, make sure to update your regex pattern:

$pattern = sprintf('\<p\sclass=\"myprefix-%s\"\>(.*?)\<\/p\>', '[0-9]+');

REPL.IT DEMO