judeddokoinu Posted April 8, 2006 Share Posted April 8, 2006 Hi, I've got a string that's about 5 kilobytes long, so there is lots of things in it.What I need to do is loop through the string and find occurrences of another string, then backtrack and save text before, inside and after the occrrence.EX:Given the string:[code]$haystack = "the quick brown fox jumped over the fence. the quick brown fox jumped over the hill. the quick brown fox jumped over the hole.";[/code]And I wanted to find the entire sentence that contained the word "hill".[code]$needle = "hill";[/code]How would I get the output:[code]$pincushion = "the quick brown fox jumped over the hill.";[/code]And assume that the first portion of the sentence is not always the same. Some days it will be:[code]$pincushion = "the slow red hen jumped over the hill.";[/code]I also need to preserve the original string, as there will be multiple searches made.I do know a bit about PHP, but I'm fairly new to the finer points of string functions. Any help would be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/6897-parsing-a-string/ Share on other sites More sharing options...
mistergoomba Posted April 8, 2006 Share Posted April 8, 2006 A couple suggestions:The easy way would be to separate the strings first.[code]$haystack = explode(".",$haystack);[/code]Then you can use 'foreach' to search each individual sentence.The hard way is to learn how to use Regular Expressions. In this case, the easy way would probably be best, but it wouldn't hurt to learn Regular Expressions anyway. They will definitely come in handy. Quote Link to comment https://forums.phpfreaks.com/topic/6897-parsing-a-string/#findComment-25083 Share on other sites More sharing options...
judeddokoinu Posted April 8, 2006 Author Share Posted April 8, 2006 Now let me throw a wrench in the gears:Suppose the string were HTML, and I were looking for sections of the code... What delimiter would I split it based on? An angle bracket, or maybe a full tag?Currently, I'm using substr() which doesn't work very well, seeing as how the size of the words change from time to time...**EDIT**I think that using explode on "<" will work... Just have to remember to add the < back into it. It will at least help, anyway.Now that I have them split, I will have to find the string inside of the bigger string *sweat***EDIT**Okay, I now have an array containing all of the elements, since they begin with "<". You suggested foreach(), and reading up on it, it says that it loops through an array, and can match against expressions. This is where the trouble begins, I think....I have a shorter string, but still don't know what expression to use to determine if a substring is located inside of a larger string. Quote Link to comment https://forums.phpfreaks.com/topic/6897-parsing-a-string/#findComment-25084 Share on other sites More sharing options...
mistergoomba Posted April 8, 2006 Share Posted April 8, 2006 use strpos to test the occurance of a string in an array.so you have your array. let's say it looks something like this:[code]$tags = Array [0] = 'a href=dir1/link1.html>' [1] = 'a href=dir2/link2.html>' [2] = 'img src=dir2/img1.jpg>'[/code]we want to find all tags that use the directory 'dir2'. now, use foreach to search the array.[code]foreach($tags as $k=>$v){ if(strpos($v,'dir2/')){ echo 'Tag #'.$k.' contains the directory!'; }}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/6897-parsing-a-string/#findComment-25096 Share on other sites More sharing options...
judeddokoinu Posted April 9, 2006 Author Share Posted April 9, 2006 Thank you, goomba, that was what I was looking for! Quote Link to comment https://forums.phpfreaks.com/topic/6897-parsing-a-string/#findComment-25117 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.