Jump to content


Photo

Parsing A String


  • Please log in to reply
4 replies to this topic

#1 judeddokoinu

judeddokoinu
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 08 April 2006 - 08:57 PM

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:
$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.";
And I wanted to find the entire sentence that contained the word "hill".
$needle = "hill";
How would I get the output:
$pincushion = "the quick brown fox jumped over the hill.";
And assume that the first portion of the sentence is not always the same. Some days it will be:
$pincushion = "the slow red hen jumped over the hill.";
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.

#2 mistergoomba

mistergoomba
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 08 April 2006 - 09:01 PM

A couple suggestions:

The easy way would be to separate the strings first.

$haystack = explode(".",$haystack);

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.

#3 judeddokoinu

judeddokoinu
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 08 April 2006 - 09:07 PM

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.

#4 mistergoomba

mistergoomba
  • Members
  • PipPip
  • Member
  • 16 posts

Posted 08 April 2006 - 10:14 PM

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:

$tags = Array
     [0] = 'a href=dir1/link1.html>'
     [1] = 'a href=dir2/link2.html>'
     [2] = 'img src=dir2/img1.jpg>'

we want to find all tags that use the directory 'dir2'. now, use foreach to search the array.

foreach($tags as $k=>$v){
     if(strpos($v,'dir2/')){
          echo 'Tag #'.$k.' contains the directory!';
     }
}


#5 judeddokoinu

judeddokoinu
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 09 April 2006 - 12:09 AM

Thank you, goomba, that was what I was looking for!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users