473,387 Members | 1,497 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

eregi or similar_text < -- problem either way

(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
5 2744
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: fartsniff | last post by:
Hello all. I am starting to work on a URL "cleaner" of sorts. The code below is only checking for a few simple entries on the URL, but for some reason it is not replacing them with "" when...
5
by: Jane Doe | last post by:
Hi I took a quick look in the archives, but didn't find an answer to this one. I'd like to display a list of HTML files in a directory, showing the author's name between brackets after the...
0
by: renster | last post by:
Hi, I was wondering if anyone here had a decent solution to this mysql/php problem. I have a mysql database with a list of groups (group_id, group_name, group_location, group_time,...
1
by: Stephan Heckmueller | last post by:
Hallo, in der Dokumentation von o.g. Funktion wird eine Arbeit von Oliver aus dem Jahre 93 erwaehnt; kann mir jemand den genauen Titel mitteilen? In welcher Datei des PHP-Quellcodes ist die obige...
2
by: Frank | last post by:
I'm having trouble detecting whitespaces in strings. Set up this test: echo "<br>example 1:".intval(eregi("^\s","teststring")); echo "<br>example 2:".intval(eregi("^\s","test string")); ...
25
by: Dynamo | last post by:
Hi The following script was taken from John Coggeshall's (PHP consultant) in his article on Zends site at http://www.zend.com/zend/spotlight/ev12apr.php // Get the email address to validate...
1
by: news | last post by:
God, I have read every comment in php.net eregi and Google searched, and I have tried so many different attempts...this is the closest I've gotten to verify a variable contains only:...
4
by: Nel | last post by:
Hi all, I am struggling with understanding a small eregi problem in php4. My code: <?PHP $htmlsource = '<img src="pics/hotdog.gif"> text text <img src="pics/silly%20sausage.gif"> ';...
1
by: fade2gray | last post by:
Hi, (new to group and a php novice) I'm editing some files using exapmles for reference. (1) // if (!eregi("admin.php", $_SERVER)) { die ("Access Denied"); } (2) if ( !defined('ADMIN_FILE')...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.