Jump to content

Parsing A String


judeddokoinu

Recommended Posts

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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]
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.