PHP Help Please

Status
Not open for further replies.

bam bam

New member
Oct 9, 2007
962
11
0
Anyone know a way to scrape a page for certain words that could appear at certain distances apart, but might also be right next to each other? In the kind of vein as We Feel Fine / by Jonathan Harris and Sep Kamvar would do.
I've got a feeling i'm gonna need to learn regex, right?

i.e.
Searching for "John + Likes" in: "I heard that John likes to play with bumble bees"
Would return: "to play with bumble bees"

BUT ALSO

Searching for "John + Likes" in: "John said he likes to play with bumble bees"
Would return: "to play with bumble bees"

Any ideas?
 


Anyone know a way to scrape a page for certain words that could appear at certain distances apart, but might also be right next to each other? In the kind of vein as We Feel Fine / by Jonathan Harris and Sep Kamvar would do.
I've got a feeling i'm gonna need to learn regex, right?

i.e.
Searching for "John + Likes" in: "I heard that John likes to play with bumble bees"
Would return: "to play with bumble bees"

BUT ALSO

Searching for "John + Likes" in: "John said he likes to play with bumble bees"
Would return: "to play with bumble bees"

Any ideas?

I know it's regex, but sadly I'm horrible with regex.
 
This should do the trick:
Code:
<?php

$str1 = "I heard that John likes to play with bumble bees";
$str2 = "John said he likes to play with bumble bees";

$find1 = "John";
$find2 = "likes";

if (eregi (".*".$find1.".*".$find2."(.*)", $str2, $result)) {
    echo $result[1];
} else {
    echo "Not found";
}
if (eregi (".*".$find1.".*".$find2."(.*)", $str1, $result)) {
    echo $result[1];
} else {
    echo "Not found";
}
?>

If you want me to explain it, pm me or contact me on IRC (nick:Pryda).
 
  • Like
Reactions: bam bam
Anyone know a way to scrape a page for certain words that could appear at certain distances apart, but might also be right next to each other? In the kind of vein as We Feel Fine / by Jonathan Harris and Sep Kamvar would do.
I've got a feeling i'm gonna need to learn regex, right?

i.e.
Searching for "John + Likes" in: "I heard that John likes to play with bumble bees"
Would return: "to play with bumble bees"

BUT ALSO

Searching for "John + Likes" in: "John said he likes to play with bumble bees"
Would return: "to play with bumble bees"

Any ideas?

First, I'd be looking for something like
/John likes (to .*?)./
This would capture "to play with bumble bees" in $matches[1] in this line
$count=preg_match_all( $pattern, $subject, $matches );
Then I'd be trying a second pass with
/John .*? likes (to .*?)./

I'm not great with regex myself, but this could get you started. You might want to play with it a bit on the second one to make sure you don't have any periods between John and likes ( escape the dot!).
 
  • Like
Reactions: bam bam
Try something like
PHP:
preg_match('/john\s.*?likes\s(.+)\./i', $yourstring, $matches);
$partyouwanttomatch = $matches[1];

Have tested it on a regex tester, but haven't run the code to see if it actually does exactly what you want. I strongly suggest that you learn regular expressions, because they're very useful when crunching textual data. You can take a look here perlre - perldoc.perl.org for a full explanation of them (its Perl oriented, but they're interchangeable with PHP and all other regexes...)
 
Try something like
PHP:
preg_match('/john\s.*?likes\s(.+)\./i', $yourstring, $matches);
$partyouwanttomatch = $matches[1];

Have tested it on a regex tester, but haven't run the code to see if it actually does exactly what you want. I strongly suggest that you learn regular expressions, because they're very useful when crunching textual data. You can take a look here perlre - perldoc.perl.org for a full explanation of them (its Perl oriented, but they're interchangeable with PHP and all other regexes...)

Where's Hassan's new chick when you need her? I hear she's crunchy :)
 
Woah, well that was unexpected! haha
Thanks alot people, very much appreciated.

You've all given me a warm fuzzy feeling inside.
*fap fap fap fap fap fap fap!*

There, it's gone again now.
 
bam bam i added you on msn yesterday im mjrpbplayer- logon puh-leez
 
Last edited by a moderator:
The regex solutions above will work fine but would allow an infinte amount of text between the two words you are searching. Between the fords, it'd be ideal to look for a limited number of word boundries or white space to ensure the words are close to together. You could also require that sentence ending puncuation (periods, exclamations, question marks) does not appear between the words.
 
Status
Not open for further replies.