Networking-Forums.com

Professional Discussions => Programming Goodies and Software-Defined Networking => Topic started by: nikolaymartin on May 05, 2020, 09:03:05 AM

Title: Match for a word in a text with the PHP script
Post by: nikolaymartin on May 05, 2020, 09:03:05 AM
I have an applications developed with PHP. I have a big text file where I want to match for a word but this can be change in the feature. How can I match a word in PHP?
Title: Re: Match for a word in a text with the PHP script
Post by: deanwebb on May 05, 2020, 10:08:32 AM
Is this where the word you want to match for changes? I'm not understanding the question.
Title: Re: Match for a word in a text with the PHP script
Post by: Otanx on May 05, 2020, 11:22:12 AM
Would need more details on what exactly you are trying to do. Just a True/False word is in text? I don't know about PHP, but python would be something like;


long_text = "This is the text that you are searching to find the specific text you want in this wordy string"
find_word = " word "
print(find_word in long_text)


That would return False because the word "word" does not exist in that string. Note that the find_word variable has spaces around the word. This prevents it from matching "wordy" If you left the spaces out it would match if it found those four characters in a row.

May I ask why PHP? Python seems to be the preferred language now which is why I am learning it.

-Otanx
Title: Re: Match for a word in a text with the PHP script
Post by: edizgeorgi on May 06, 2020, 07:32:58 AM
There are different functions and ways to match a word in PHP. Best way is using preg_match() function. In the following example we will search the forum word in the $bigtext content.
preg_match('/forum/',$bigtext)
Look: https://www.networking-forums.com/programming-goodies-and-software-defined-networking/match-for-a-word-in-a-text-with-the-php-script