p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Can you use variables in regular expressions?


Message #1 by "John Arbon" <subscriptions@c...> on Sun, 24 Feb 2002 01:19:19
Is it possible to use variables in a regular expression?



If so, why do I get a parse error for the following code?



<?php

	$testfile = "perl/test_file.txt";

	$wordfile = "perl/word_file.txt";



	$Htestfile = fopen($testfile, "r") or die ("Ouch!");

	$Hwordfile = fopen($wordfile, "r") or die ("No words!");



	$tfilecontents = fread($Htestfile, filesize($Htestfile));

	$wfilewords = fread($Hwordfile, filesize($Hwordfile));



	fclose($Htestfile);

	fclose($Hwordfile);



	$tseparated = explode(" ", $tfilecontents);

	$wseparated = explode(" ", $wfilewords);

	

	foreach($tseparated as $sub_word)

	{

		foreach($wseparated as $test_word)

		{

			if(preg_match("/\b$test_word/i", $sub_word)

// Error on this line	{

				echo "Match found\n";

			}

		}

	}

?>

  Return to Index