desmondMorris Posted November 27, 2009 Share Posted November 27, 2009 Hi, Im quite new to PHP, i wanted to know if it would be possible to have a text box that allows a user to add some text and then have PHP read through the text and if it matched one of many different things that i have defined to output a html file with only certain parts of the text they inputted to be shown. for example if they put 'my name is bob, and i like the color blue' and this matched my setting of 'my name is:' and 'color' to output a html/php file that says 'hello bob, blue is a nice color'. i would want to be able to pull out certain words from the text box as long as they are preceeded by the words i have set. i dont know if this is possible. thanks in advance any help is appreciated. Des Quote Link to comment https://forums.phpfreaks.com/topic/183112-help-with-matching-text/ Share on other sites More sharing options...
JAY6390 Posted November 27, 2009 Share Posted November 27, 2009 Yeah you can do this with regular expressions (regex) $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!"; Quote Link to comment https://forums.phpfreaks.com/topic/183112-help-with-matching-text/#findComment-966403 Share on other sites More sharing options...
desmondMorris Posted November 27, 2009 Author Share Posted November 27, 2009 thanks, thats a massive help. Quote Link to comment https://forums.phpfreaks.com/topic/183112-help-with-matching-text/#findComment-966405 Share on other sites More sharing options...
0perator Posted November 27, 2009 Share Posted November 27, 2009 might want to experiment with regular expressions, another viable, yet more laborious method would be to explode the string and search through each key until you find "bob" then check the previous keys to see if it matches what you want. Quote Link to comment https://forums.phpfreaks.com/topic/183112-help-with-matching-text/#findComment-966407 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.