How to read a page’s “Shortcodes” from the Template File?

Parsing shortcodes from strings

WordPress parses and replaces shortcodes from a piece of content via the do_shortcode function. This function, in turn, calls get_shortcode_regex, which returns the regular expression for matching shortcodes in a string.

Using this function, we can get a list of all shortcodes ourselves:

$pattern = get_shortcode_regex();
preg_match_all( "/$pattern/s", $content, $matches );
print_r( $matches );

preg_match_all puts all matches in $content for our regular expression in $matches. $matches will then contain a two-dimensional array. For a string like this, for example:

[first-shortcode no-content="true"]
This is shortcode testing content.
[sample-shortcode title="Testing"]This is a test[/sample-shortcode]

(assuming the shortcodes first-shortcode and sample-shortcode exist), print_r( $matches ); would output:

Array
(
    [0] => Array
        (
            [0] => [first-shortcode no-content="true"]
            [2] => [sample-shortcode title="Testing"]This is a test[/sample-shortcode]
        )

    [1] => Array
        (
            [0] => 
            [1] => 
        )

    [2] => Array
        (
            [0] => first-shortcode
            [1] => sample-shortcode
        )

    [3] => Array
        (
            [0] =>  no-content="true"
            [1] =>  title="Testing"
        )

    [4] => Array
        (
            [0] => 
            [1] => 
        )

    [5] => Array
        (
            [0] => 
            [1] => This is a test
        )

    [6] => Array
        (
            [0] => 
            [1] => 
        )
)

As you can see, for all matches (shortcodes), there is an entry in each of the seven arrays at the first level. $matches[0] contains the entire shortcode, $matches[2] contains the shortcode name, $matches[3] the attributes string and $matches[5] the shortcode content.

I’m not sure what part you wish to manipulate, so I won’t go into detail on that. You can pass the flag PREG_OFFSET_CAPTURE to preg_match_all in the $flags-parameter; this will make sure the offset at which a match (shortcode) occurs in the string is stored as well.

Getting the content of a page template

To get the content of a template file (which could be a page template) of the current theme, you can simply use locate_template, which returns the path to a template file, after which you can get its contents by using an output buffer.

ob_start();
locate_template( 'tpl-my-page-template.php', true );
$contents = ob_get_contents();
ob_end_clean();

I’m not exactly sure what you’re trying to achieve, but the first part of my answer is at least part of what you need :-).