Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.