x1705 Posted June 9, 2020 Share Posted June 9, 2020 (edited) 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 June 9, 2020 by x1705 better description. Quote Link to comment https://forums.phpfreaks.com/topic/310909-help-finding-a-matching-word-within-a-phrase-of-text-using-a-string/ Share on other sites More sharing options...
x1705 Posted June 9, 2020 Author Share Posted June 9, 2020 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; Quote Link to comment https://forums.phpfreaks.com/topic/310909-help-finding-a-matching-word-within-a-phrase-of-text-using-a-string/#findComment-1578771 Share on other sites More sharing options...
Barand Posted June 9, 2020 Share Posted June 9, 2020 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/310909-help-finding-a-matching-word-within-a-phrase-of-text-using-a-string/#findComment-1578772 Share on other sites More sharing options...
x1705 Posted June 10, 2020 Author Share Posted June 10, 2020 (edited) 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 June 10, 2020 by x1705 Quote Link to comment https://forums.phpfreaks.com/topic/310909-help-finding-a-matching-word-within-a-phrase-of-text-using-a-string/#findComment-1578786 Share on other sites More sharing options...
benanamen Posted June 10, 2020 Share Posted June 10, 2020 What is the real problem you are trying to solve by doing this? What is the high level overview of what you have going on? Quote Link to comment https://forums.phpfreaks.com/topic/310909-help-finding-a-matching-word-within-a-phrase-of-text-using-a-string/#findComment-1578789 Share on other sites More sharing options...
x1705 Posted June 10, 2020 Author Share Posted June 10, 2020 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. Quote Link to comment https://forums.phpfreaks.com/topic/310909-help-finding-a-matching-word-within-a-phrase-of-text-using-a-string/#findComment-1578793 Share on other sites More sharing options...
x1705 Posted June 10, 2020 Author Share Posted June 10, 2020 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 Quote Link to comment https://forums.phpfreaks.com/topic/310909-help-finding-a-matching-word-within-a-phrase-of-text-using-a-string/#findComment-1578794 Share on other sites More sharing options...
x1705 Posted June 10, 2020 Author Share Posted June 10, 2020 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! Quote Link to comment https://forums.phpfreaks.com/topic/310909-help-finding-a-matching-word-within-a-phrase-of-text-using-a-string/#findComment-1578795 Share on other sites More sharing options...
benanamen Posted June 10, 2020 Share Posted June 10, 2020 This page may be of help to you. We all started at the beginning. http://www.catb.org/~esr/faqs/smart-questions.html Quote Link to comment https://forums.phpfreaks.com/topic/310909-help-finding-a-matching-word-within-a-phrase-of-text-using-a-string/#findComment-1578800 Share on other sites More sharing options...
x1705 Posted June 11, 2020 Author Share Posted June 11, 2020 Perfect, thanks for that.. (link) yes. I think that is a common issue, understanding. Quote Link to comment https://forums.phpfreaks.com/topic/310909-help-finding-a-matching-word-within-a-phrase-of-text-using-a-string/#findComment-1578825 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.