By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,911 Members | 1,161 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,911 IT Pros & Developers. It's quick & easy.

eregi or similar_text < -- problem either way

P: n/a
(driving me nuts)
Hi there. I wonder if anyone can help?

I'm including a page from Google in search.php, passing some
parameters. So far so good. Then I'm asking to look through that
Google page for a text match, and return true or false.
eregi returns false whatever the case,
similar_text returns true whatever the case.

Can someone sort me out - oops, HELP me out ?
Thanks thanks in advance !

(code)
$page="http://www.google.com/search$search";
// check to see if on page 1
$text="my_text_here";
if (eregi('/\b$text/i', '$page')) {
echo "Match";
} else {
echo "No Match";
}
(/code)

OR

(code)
$page="http://www.google.com/search$search";
// check to see if on page 1
$text="my_text_here";
if (similar_text('/\b$text/i', '$page')) {
echo "Match";
} else {
echo "No Match";
}
(/code)
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
george wrote:
$page="http://www.google.com/search$search";
// check to see if on page 1
$text="my_text_here";
if (eregi('/\b$text/i', '$page')) {
echo "Match";
} else {
echo "No Match";
}
Never matches.

You are checking for
a slash followed by a back slash, b, dollar sign, t, e, x, t, slash, i
in
dollar sign, p, a, g, e
If you want to check for
backslash, b, m, y, underscore, t, e, x, t, underscore, h, e, r, e
in
h, t, t, p, colon, slash, slash, ...

the right eregi() is
eregi('\b' . $text, $page)
The slashes and last "i" you put in the eregi are used for preg_*
functions (which are better than their ereg* counterparts).

$page="http://www.google.com/search$search";
// check to see if on page 1
$text="my_text_here";
if (similar_text('/\b$text/i', '$page')) {
echo "Match";
} else {
echo "No Match";
}


In my tests I only managed to have similar_text() return 0 (false) with
similar_text('', $something) or similar_text($something, '')
Any two non-empty strings will return at least 1 (true).

The strings you are comparing are here are
slash, backslash, b, dollar sign, t, ...
and
dollar sign, p, a, g, e

To compare
m, y, underscore, t, e, x, t, underscore, ...
and
h, t, p, p, colon, slash, slash, ...

the right similar_text() is
similar_text($text, $page)
NOTE: none of these "corrected" things will check whether
the *contents* of the web page contain whatever is in $text
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #2

P: n/a
george wrote:
if (eregi('/\b$text/i', '$page'))


A couple of important points:

(i) You're using a PCRE pattern in a POSIX function, and the
delimiters, assertion, variable and modifier aren't
understood.

(ii) You're expecting variable expansion in single-quotes, in at
least two places:
(a) the pattern parameter, and
(b) the subject parameter.

Ref.:

http://www.php.net/manual/en/function.preg-match.php

--
Jock
Jul 17 '05 #3

P: n/a
Thanks guys, especially Pedto, for being so lucid.

Neither worked though.

All I'm hoping to do is
1. include a webpage and display it <-- which is ok
2. Run down the page looking for a text string, and
3. If it's there display "Match found"

On my own attempts nothing works.

Neither:
if (similar_text($text, $page)) {
-or-
if (eregi('\b' . $text, $page)) {

I have a quick nasty fix running using javascript but it's ugly.
Any further thoughts would be appreciated.

See ya
Jul 17 '05 #4

P: n/a
george wrote:
All I'm hoping to do is
1. include a webpage and display it <-- which is ok
Since you want the data for something more than just display it, do not
include it, instead put its contents in a variable

$URL = 'http://www.example.com/';
$contents = file_get_contents($URL); // see [1] below
// and now that the webpage is saved display it
echo $contents;

2. Run down the page looking for a text string, and
$text_string = 'text string';
if (stripos($contents, $text_string) !== false) { // see [2] below
3. If it's there display "Match found"


echo 'Match found';
}
[1] http://www.php.net/file_get_contents
use another method to get the file contents if your php < 4.3.0
fopen() and fread() should do it [don't forget fclose() if you
choose to use fopen()]

[2] http://www.php.net/stripos
stripos() performs a case insensitive search; if you need a case
sensitive search use strpos() instead
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #5

P: n/a
THANK YOU PEDRO
...
Because the server is running <PHP5, I got an undefined function on stripos().
But this did the trick:

function stripos($contents, $text_string, $offset=0) {
return strpos(strtoupper($contents), strtoupper($text_string), $offset);
}

if (stripos($contents, $text_string) !== false) {
echo "Match";
} else {
echo "Duh!";
}
Thank you very very much.
Bye
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.