I have a string that contains "%tokens%" that are replaced with str_replace() with corresponding information pulled from a database

$string = "content content content %name% content %number% more content content %name% %name% %name% %name% ...etc";
//outputs: content content content Sarah content 555-555-5555 more content content Sarah ...etc

Thanks ll dandy, but I'm looking for is some sort of way to extract all the unique tokens (%i[/i]%) from the string, and put them into an array, so that I can cross compare w/ the available data for that particular person.

I've done,

function escapepercent($string) {
	$string = str_replace("%", "\%", $string);
	return $string;
}
function removeslashes($string) {
	$string = str_replace("\%", "", $string);
	return $string;
}

//explode words
$words = explode(" ", $string);

$tokens = "";
$i = 0;
foreach ($words as $word) {
	$word = escapepercent($word);
	//if word contains %
	if (strpos($word, "%")) {
		$word = removeslashes($word);
		//check if the token is already in the array
		if (!in_array($word, $tokens)) {
			//if its not, add the token (ie; "number")
			$tokens[$i] = $word;
			$i++;
		}
	}
}		

print_r($tokens);

Which worked fine with the string above, but when I used a more practical string like

$string = "
<html>
<head>
<style type=\"text/css\">
<!--
.BODYWRAP {
	height: 10in;
	width: 7.5in;
	page-break-after: always;
}
-->
</style>
</head>
<body>
<div class=\"BODYWRAP\">Practical Example of ContentThis is some practical 
example of content for the recipient %FirstName% %LastName%, living at 
%Address% %City%, %Province%, %Country% %Postal%, whose home number 
is %Home%.</div>
</body>
</html>";

The array $tokens, looks like

Array
(
    [0] => FirstName
    [1] => LastName[COLOR="Red"],[/COLOR]
    [2] => Address
    [3] => City[COLOR="red"],[/COLOR]
    [4] => Province[COLOR="red"],[/COLOR]
    [5] => Country
    [6] => Postal[COLOR="red"],[/COLOR]
    [7] => Home[COLOR="red"].</div>
</body>
</html>





		Page[/COLOR]
[8] => Home[COLOR="red"].</div>
</body>
</html>



[/COLOR]
)

which show how useless what I've done is..

Is there anyways to extract unique words (within %here%) and put them into an array?

    i do something quite similar, i suggest you make your db fields names the same as your tokens (or alias them) then its a dawdle to foreach loop through the db result and do a string replace.

      preg_match_all('#(?<=%)[^%\s]+(?=%)#', $string, $matches);
      $words = array_unique($matches[0]);
      
      echo "<pre>";
      print_r($words);
      echo "</pre>";
      
        Write a Reply...