Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Regex Question


ghost's Avatar
0 0

Hey can you guys help me out with this code? Basically I want to check a site for part of a word and then echo out the entire word. So for example say I have:

$url = file_get_contents("http://www.google.com","r");
$pattern = '/ages/';
if (preg_match($pattern,$url))
{
echo "its here";
}
else
{
echo "not here";
}
?>```

I'm looking for the string "ages" and I then want to echo out the full word, which, in this case, would be images.  It's easy enough to see if the string exists on the site, but I have no idea how to get the whole word.  Do you guys have any ideas how to do this? Thanks

ghost's Avatar
0 0

That's very… Stupid code. Anyway.

I agree, but it's simple which is why I gave it as an example… Are you saying just to replace markup$pattern = '/ages/'; to markup$pattern = "/([a-zA-Z]*)ages/"; I'm not trying to just see if a word ending with 'ages' exists on the page (if i was I would just change the pattern to '/[a-z]*ages/i', which is basically what you had). I want to first see if the string 'ages' exists on the page, and then if it does, store the whole word that the string belongs to into a variable.

If for some reason I'm still not clear about what I'm trying to do (or if I'm not getting what you're saying), post back and I'll try to explain it again.


spyware's Avatar
Banned
0 0

Scan for the word ages, if it exists, scan for the two spaces "beside" the word and include everything from space #1 before "ages" to space #2 after "ages".


ghost's Avatar
0 0

Alright thanks spyware