Jump to content

Help with matching text


desmondMorris

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/183112-help-with-matching-text/
Share on other sites

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!";

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.