Jump to content

textbox confusion


desmondMorris

Recommended Posts

Ive currently got a code to pull out certain words from a piece of text based on the preceding word

 

<?php

$text = 'My name is Jay and I like the color blue';

if(preg_match('%my name is (?P<name>\w+)%i', $text, $out)) {

    $name = $out['name'];

}

if(preg_match('%color (?P<color>green|red|orange|yellow|pink|black|white|blue)%i', $text, $out)) {

    $color = $out['color'];

}

echo '<pre>'.print_r(get_defined_vars(), true).'</pre>';

if(isset($name) && isset($color)) {

    echo "Hello $name, $color is a nice color!";

}

 

?>

 

however i would like to be able to add the text through a textbox rather than have the text written into the code, whenever i try to do this, nothing is displayed. does anyone know how i should be going about it?

 

thanks in advance for any help

 

des

Link to comment
https://forums.phpfreaks.com/topic/187711-textbox-confusion/
Share on other sites

for that you have to make a form

<?php

if(isset($_POST['submit'])){

 

$text = $_POST['txt'];

if(preg_match('%my name is (?P<name>\w+)%i', $text, $out)) {

    $name = $out['name'];

}

if(preg_match('%color (?P<color>green|red|orange|yellow|pink|black|white|blue)%i', $text, $out)) {

    $color = $out['color'];

}

//echo '<pre>'.print_r(get_defined_vars(), true).'</pre>';

if(isset($name) && isset($color)) {

    echo "Hello $name, $color is a nice color!";

}

 

 

}

?>

<form name='testForm' action='' method='post'>

<input type='text' name='txt' value=''>

<input type='submit' name='submit' value='submit'>

</form>

 

 

Link to comment
https://forums.phpfreaks.com/topic/187711-textbox-confusion/#findComment-991030
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.