Jump to content

help finding a matching word within a phrase of text. using a string.


x1705

Recommended Posts

Hi, and thanks for any help with this. 

I have this but of php i have been trying relentlessly to get working.. for over a month.
 

<?php
$abs="one two three four words";
$mT = "testing for matching words";

$words=explode(' ', $abs);
//$sf=($words);


if (preg_match($words, $mT))
{
        echo "the url $mT contains a Word";
}
else
{
        echo "the url $mT does Not contain a Word";

echo "$words[4]";

}
?>

You see, I get a responce only using the last line "$words[4]", (obviously, because I am pointing it the Matching word, (ie: with [4])
What i need to have is that the script will automatically "find" that Matching Word for me with ANYwhere of the string of $abs (not just the beginning or the ending)". 

not making it confusing, but the obvious word in this I am looking to find is the word, "words".
😕

Thanks so much for any help.


 

 

Edited by x1705
better description.
Link to comment
Share on other sites

I've (or am trying now) to add something like this: After the "preg_match" line.
thinking somehow it needs to 'loop' through the string.. any better suggestions much appreciated.

foreach($words as $v) {
  if(stripos($mT, $v)) {
//Found a word

$answer=$v;
    break;

 

Link to comment
Share on other sites

If you are looking for words that are common to both $abs and $mT

<?php
$abs="one two three four words";
$mT = "testing for matching words";

$absArr = explode(' ', $abs);
$mTArr  = explode(' ', $mT);

$common = array_intersect($absArr, $mTArr);

// check result
echo '<pre>', print_r($common, 1), '</pre>';
  
?>
Link to comment
Share on other sites

Perfect, that works.. now is it possible to make just the string it finds, "words" into a seperate string itself?

I am looking into imploding it back? I will do some more reading, but that is what i am looking for.

 

I believe this is all i needed to do.. let me know if you have a better (for some reason) solution.
thank you!!

Quote

echo implode(' ', $common);

*tip sent btw.. Thank you.

 

 

 

Edited by x1705
Link to comment
Share on other sites

Thanks for asking,  I have a Bot for Facebook Messenger that i use, and in a group chat, say someone is talking and they use a "keyword" that the bot will then interject some information on that "keyword".. sounds simple, ;) It is highly complex in all.. as that is not all that it does, but that is what i am working on right now. It is not the "high end/ai" type bot with buttons.. more like an old school "IRC" Scripted one.. with whistles. :)  thats my opinion anyways.  It is not for professional use at all. it is just a hobby of mine that i enjoy with some people that i know. I believe the code provided from here is working, i just have to implement it correctly into the large PHP script that the bot uses now.. just takes me some (or a lot) of 'fidgeting' :)

so, in essence; it would look somehting like this:

me: good morning
bot: {shows a forecast, or time - for example}
someone: hey, thanks..
bot: {smiles}

kind of 'random', but i think you get the picture.

:)

Link to comment
Share on other sites

if $mT Does_Not contain a matching word(s),
I still need it to carry that value to the end.

This is the way it is working, but only when it matches a word.

Quote

<?php
$abs="one two three four words";
$mT = "testing for matching words";

// if $mT Does_Not contain a matching word(s), 
// I still need it to carry that value to the end.

$absArr = explode(' ', $abs);
$mTArr  = explode(' ', $mT);

$common = array_intersect($absArr, $mTArr);
$answer =  implode(' ', $common);


echo $answer;  
?>


some use of an 'if' / 'else if' statement perhaps?

I know this is probably pretty basic, for most anyone, so i appreciate it.
Thanks :)
 

 

 

Link to comment
Share on other sites

Sorry about the last request/post, i think it was my error, it does work.. just took some time to insert it correctly within the current php.
I appreciate the help!

 

and I noted Benanamen's TAGLine;


 

Quote

The XY Problem
The XY problem is asking about your attempted solution (X) rather than your actual problem (Y). This leads to enormous amounts of wasted time and energy, both on the part of people asking for help, and on the part of those providing help.

Dually noted. lol
That would be nice, but in my php unenlightened mind, It is hard to phrase the "problem", half or mostof the time.. i don't KNOW what the Problem is..
and when that is apparent, it is finding the "proper" way to explain something/ask in a way that i do not understand, hence.. the Question. :)
But, i get what you are saying.. truly. I htink forming the proper Question is the hardest part for newbies. :)  Thank you, i appreciate your help! :)

 

Link to comment
Share on other sites

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.