<?php
$regex = '~{[^}]+}|"Tarzan\d+"|(Tarzan\d+)~';
$subject = 'Jane" "Tarzan12" Tarzan11@Tarzan22 {4 Tarzan34}';
$count = preg_match_all($regex, $subject, $m);

// build array of non-empty Group 1 captures
$matches=array_filter($m[1]);

///////// The six main tasks we're likely to have ////////

// Task 1: Is there a match?
echo "*** Is there a Match? ***<br />\n";
if(empty($matches)) echo "No<br />\n";
else echo "Yes<br />\n";

// Task 2: How many matches are there?
echo "\n<br />*** Number of Matches ***<br />\n";
echo count($matches)."<br />\n";

// Task 3: What is the first match?
echo "\n<br />*** First Match ***<br />\n";
if(!empty($matches)) echo array_values($matches)[0]."<br />\n";

// Task 4: What are all the matches?
echo "\n<br />*** Matches ***<br />\n";
if(!empty($matches)) {
	foreach ($matches as $match) echo $match."<br />\n";
}

// Task 5: Replace the matches
$replaced = preg_replace_callback(
	$regex,
	// in the callback function, if Group 1 is empty,
	// set the replacement to the whole match,
	// i.e. don't replace
	function($m) { if(empty($m[1])) return $m[0];
					else return "Superman";},
	$subject);
echo "\n<br />*** Replacements ***<br />\n";
echo $replaced."<br />\n";

// Task 6: Split
// Start by replacing by something distinctive,
// as in Step 5. Then split.
$splits = explode("Superman",$replaced);
echo "\n<br />*** Splits ***<br />\n";
echo "<pre>"; print_r($splits); echo "</pre>"; 
