--------------------------------------------------------------------------------

I am trying to create a madlibs game ..

I have a txt file with paragraphs in it .. Some of the sentences have the following tags in it <#noun>, or <#verb>, etc ...

I need to parse the file in php and display the content in between the tags to the user.

The user can fill the the words in text box, and the story will be displayed with the new words instead of the tags.

I have spent lots of time on google and only got more confused.

I would really appreciate if anyone can put this into php code, and I can play around as needed.

Here is an example of the text file:

One day, I was walking through the <# adjective #> part of
Toronto when I ran into a huge stack of <# plural noun #>!
It was the most <# adjective #> sight I had ever seen. I tried
to call my best friend, <# proper noun #>, to tell <# him/her #>
about it but all of the phone circuits were busy.

Thanks in Advance,
Vladimir

I'm relatively new to these boards - but most PHP forums don't simply do your work for you. You need to provide some code & evidence of what you've tried yourself rather than get someone on the Internet to do your work for you.

Usually I agree with quasipickle about showing effort, but your thread is interesting and I am really bored, so I wrote a quick example on how to do it.

This is just a very basic way of doing things.

<?php

$data =<<<MADLIB
One day, I was walking through the <# adjective #> part of
Toronto when I ran into a huge stack of <# plural noun #>!
It was the most <# adjective #> sight I had ever seen. I tried
to call my best friend, <# proper noun #>, to tell <# him/her #>
about it but all of the phone circuits were busy.
MADLIB;

//the above would come from your file, I just did it this way to save some time.

preg_match_all( '/<# ([a-z \/]+) #>/',$data,$matches );

$ndata = $data;
$i = 0;
foreach( $matches[0] as $match ) {
	if ( ( $pos = strpos( $ndata,$match ) ) === false ) {
		continue;
	}
	$ndata = substr_replace( $ndata,"<<{$i}>>",$pos,strlen( $match ) );
	$i++;
}

//could also probably do while loop with preg_replace

if ( isset( $_POST['submit'] ) ) {
	$values = array();
	$c = 0;
	foreach( $_POST as $key => $value ) {
		$value = stripslashes( trim( $value ) );
		if ( strpos( $key,'place-' ) === 0 && $value !== '' ) {
			$values[$c] = htmlentities( $_POST[$key] );
			$c++;
		}
	}
	if ( count( $values ) !== $i ) {
		die('Please fill out all the fields. <a href="' . $_SERVER['PHP_SELF'] . '">Retry</a>');
	}
	for( $i=0;$i < $c;$i++ ) {
		$ndata = str_replace( "<<{$i}>>",$values[$i],$ndata );
	}
	$html = $ndata;
}
else {
	$html  = '<form action="' . $_SERVER['PHP_SELF'] . '" method="post">'; //not recommended that you use PHP_SELF, but since its quick I will use it for the example
	$i = 0;
	foreach( $matches[1] as $datum ) {
		$html .= '<input type="text" name="place-' . $i . '" value="" />&nbsp;&nbsp;' . $datum . '<br />';
		$i++;
	}
	$html .= '<input type="submit" name="submit" value="Submit" />';
	$html .= '</form>';
}

?>
<html>
<head>
	<title>Madlib Test</title>
</head>
<body>
<?php echo $html; ?>
</body>
</html>

thanks for your help. truly appreciated

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.