I am new to PHP
This is Google search results in your site or localhost
Here is the code but I am stuck the page does not go to 2 page
Please advice

<form action="search.php" method="POST">
<input type="text" name="search">
<input type="submit" value="Search">
</form>



if (@$_POST['search']==""){
echo "What are you doing?";
}
else{
	
$query = file_get_contents("http://www.google.com/search?q=".urlencode($_POST['search'])."&num=100&hl=en&ie=UTF-8&filter=2");

//needs to be added with more queries
$replace_array = array('/\n/','/<head>(.*)<\/head>/','/(.*)<div id=res>/','/<br clear=all>(.*)/');
//pregraplacing unneeded parts
$results = preg_replace($replace_array, '', $query);
echo $results;
}
?>

The next page is not displayed
Please advice

Recommended Answers

All 25 Replies

Try the following:

<form action="search.php" method="POST">
Search: <input type="text" name="search" style="width:300px;">
<input type="submit" value="Search">
</form><?
if (@$_POST['search']==""){
echo "What are you doing?";
}
else{

$query = file_get_contents("http://www.google.com/search?q=".urlencode($_POST['search'])."&num=100&hl=en&ie=UTF-8&filter=2");

//needs to be added with more queries
$replace_array = array('/\n/','/<head>(.*)<\/head>/','/(.*)<div id=res>/','/<br clear=all>(.*)/','/(.*)\<h2 class\=hd\>Search Results\<\/h2\>/');
//pregraplacing unneeded parts
$results = preg_replace($replace_array, '', $query);
echo $results;
}
?>

Also you will need to use CSS to solve all of the styling.

The Google custom search isn’t working that fine especially when I have couple refine searches.

Still not working, appreciate your input cwarn23, your changes removed the clutter which were displaying on the top that’s a nice move, but we still cannot move to the next page,
Any idea why. I am working on the external CSS to have nice finish

Here is an updated script for you:

<style type="text/css">
A:link {font-weight:bold; font-family:"Arial";}
A:visited {font-weight:bold; font-family:"Arial";}
A:active {font-weight:bold; font-family:"Arial";}
A:hover {font-weight:bold; font-family:"Arial";}
</style>
<form action="tst2.php" method="GET">
Search: <input type="text" name="q" style="width:300px;"><input type="submit" value="Search">
</form><?
if (empty($_GET['q'])){
echo "What are you doing?";
}
else{

$query = file_get_contents('http://www.google.com/search?q='.urlencode($_GET['q']).'&start='.$_GET['p'].'00&num=100&hl=en&ie=UTF-8&filter=2');
preg_match_all('/about \<b\>([0-9,]+)\<\/b\> for/',$query,$output);

$var=$output[1][0];
unset($output);
$var=str_replace(',','',$var);
$var=$var/100;
$replace_array = array('/\n/','/.*\<h2 class\=hd\>Search Results\<\/h2\>/','/\<div class\=e\>.*/');
$results = preg_replace($replace_array, '', $query);
echo $results;
echo '<table border=0 bgcolor=#CCCCCC width=100%><tr><td align="center">';
for ($i=1;$i<7;$i++) {
    if ($i>=$var) { break; }
    echo '<a href="tst2.php?q='.$_GET['q'].'&p='.$i.'">'.$i.'</a> - ';
    }
for ($i=($_GET['p']-2);$i<($_GET['p']+4);$i++) {
    if ($i>=$var) { break; }
    if ($i>6) {
        echo '<a href="tst2.php?q='.$_GET['q'].'&p='.$i.'">'.$i.'</a> - ';
        }
    }
echo '</td></tr></table>';
}
?>

aprreciate your input on the code

this is what i get

([0-9,]+)\<\/b\> for/',$query,$output); $var=$output[1][0]; unset($output); $var=str_replace(',','',$var); $var=$var/100; $replace_array = array('/\n/','/.*\
Search Results\<\/h2\>/','/\
.*/'); $results = preg_replace($replace_array, '', $query); echo $results; echo ''; for ($i=1;$i<7;$i++) { if ($i>=$var) { break; } echo ''.$i.' - '; } for ($i=($_GET-2);$i<($_GET+4);$i++) { if ($i>=$var) { break; } if ($i>6) { echo ''.$i.' - '; } } echo '
'; } ?>

any input if i am doing wrong in placing files

Did you copy the php opening tag on the same line as the </form> tag or do you have php short tags enabled?

ok got the result displayed
let me test if there are ant errors

i saw what we are making it is amazing
what i saw search results in google displays different from our local search
any specific reason on this
the results just works fine but shows notice
is the below notice somthing common
errors/ Notice
Top: Notice: Undefined index: p in E:\first\t1\tst2.php on line 7

bottom: Notice: Undefined index: p in E:\first\t1\tst2.php on line 22

Notice: Undefined index: p in E:\first\t1\tst2.php on line 22

Notice: Undefined index: p in E:\first\t1\tst2.php on line 22

Notice: Undefined index: p in E:\first\t1\tst2.php on line 22

Notice: Undefined index: p in E:\first\t1\tst2.php on line 22

Notice: Undefined index: p in E:\first\t1\tst2.php on line 22

Notice: Undefined index: p in E:\first\t1\tst2.php on line 22

Notice: Undefined index: p in E:\first\t1\tst2.php on line 22

To solve that error, add the following code to the top of the page:

<?
if (!isset($_GET['p']) || empty($_GET['p'])) {
    $_GET['p']=0;
    }
?>

by tweaking the results are good and your code does not have any problem
The other thing which came up is this is just a generic google search.
we type what we are searching for and the results gets displayed in our localhost
now say we would go for refine search
say music
we would do something like this
visitor inputs a keyword like Whitney Houston or prince
we would do a refine search
we would add mp3 or songs but this is in the back ground working in php
example input Whitney Houston or prince + our refine search add (mp3 or songs) this refine search is hidden to visitors and gets what they are looking for.
I belive we need to do this before the search term hits google.com above line 7 or above line 2
cwarn23 I need your input on the code, its really good short and crisp, appreciate your help.

I am wroking on bing and yahooo too getting on the localhost
any inputs?

I am wroking on bing and yahooo too getting on the localhost
any inputs?

Not yet but let us know if you have any troubles.

yes actually now it is get method instead of post

Tested and tried few things i will tell you step by step

ok the first thing I was actually looking for refine search

for example:
(intitle:resume OR inurl:resume) KEYWORD "this is where the get or post "q" is going to be placed in the KEYWORD section for example if some one is looking for php resumes, so he types php in the form"

the output has to be (intitle:resume OR inurl:resume) php in the google search box but that is the backend working for us by php what we are doing is getting results in localhost. The visitor does not see the refine keywords

there has to be one more php process after the form process and before it hits google search

example here
http://www.google.com/search?hl=en&source=hp&q=%28intitle%3Aresume+OR+inurl%3Aresume%29+php&meta=&aq=f&oq=

(intitle:resume OR inurl:resume) KEYWORD "this is where the get or post "q" is going to be placed in the KEYWORD section for example if some one is looking for php resumes, so he types php in the form"

[B](intitle:resume OR inurl:resume)[/B]

So your saying that php should automatically replace the above code formatting with the below?

[B]KEYWORD resume[/B]

Easy understanding is the visitor does not type
(intitle:resume OR inurl:resume)
visitor only types "php"

but google keyword hit is (intitle:resume OR inurl:resume) php

: ):icon_smile:

to combine query "PHP"
to "(intitle:resume OR inurl:resume) php" hit google search

What we did earlier

<style type="text/css">
A:link {font-weight:bold; font-family:"Arial";}
A:visited {font-weight:bold; font-family:"Arial";}
A:active {font-weight:bold; font-family:"Arial";}
A:hover {font-weight:bold; font-family:"Arial";}
</style>
<form action="tst2.php" method="GET">
Search: <input type="text" name="q" style="width:300px;"><input type="submit" value="Search">
</form><?
if (empty($_GET['q'])){
echo "What are you doing?";
}
else{

$query = file_get_contents('http://www.google.com/search?q='.urlencode($_GET['q']).'&start='.$_GET['p'].'00&num=100&hl=en&ie=UTF-8&filter=2');
preg_match_all('/about \<b\>([0-9,]+)\<\/b\> for/',$query,$output);

$var=$output[1][0];
unset($output);
$var=str_replace(',','',$var);
$var=$var/100;
$replace_array = array('/\n/','/.*\<h2 class\=hd\>Search Results\<\/h2\>/','/\<div class\=e\>.*/');
$results = preg_replace($replace_array, '', $query);
echo $results;
echo '<table border=0 bgcolor=#CCCCCC width=100%><tr><td align="center">';
for ($i=1;$i<7;$i++) {
    if ($i>=$var) { break; }
    echo '<a href="tst2.php?q='.$_GET['q'].'&p='.$i.'">'.$i.'</a> - ';
    }
for ($i=($_GET['p']-2);$i<($_GET['p']+4);$i++) {
    if ($i>=$var) { break; }
    if ($i>6) {
        echo '<a href="tst2.php?q='.$_GET['q'].'&p='.$i.'">'.$i.'</a> - ';
        }
    }
echo '</td></tr></table>';
}
?>

now what we have to look is

after the Submit

in between process between Submit and tst2.php

we sould be looking for something like this as there are going to be many searches

<?php
$var1 ="(intitle:resume OR inurl:resume)"; 
$var2 = php; (this search term typed by visitor)

echo $var3 = $var1 . var2;

$var3 has to be link "click"

tst2.php process

Shows the result

after the Submit

in between process between Submit and tst2.php

we sould be looking for something like this as there are going to be many searches

<?php
$var1 ="(intitle:resume OR inurl:resume)";
$var2 = php; (this search term typed by visitor)

echo $var3 = $var1 . var2;

$var3 has to be link "click"

tst2.php process

Shows the result

I'll be back online tommorrow but in that quote it looks like if you posted an answer to your own problem unless I've misunderstood. So from my understanding you want the term (intitle:resume OR inurl:resume) automatically added to the search term in which case you would add the following to the top of your script:

if (isset($_GET['q'])) {
    $_GET['q'].=' (intitle:resume OR inurl:resume)';
    }

I will test this and see if it is working, It was great getting ideas from you for a baby learner. will get in touch with you tommorow
Have a great time

Good morning cwarn23, it is working but the refine search is adding up in the background hitting Google this happens when I go to the next page.
For now I have just added OR at the end on the refine search which google assumes its fine if it does not get the rest of the keywords it still displays results

If OR is removed at the end, google takes it as mandatory keyword and might not show results, this happens when I click the second page, as the refine search is adding up and not showing up results

this happens when you click the next page 2,3,4,5...

You can take a look what it is doing and also see the codes

http://careers.sytes.net/t1


You can just go at the bottom of the page click next the code adds up refine search by itself in the google search box.
Not sure how the search box got displayed but it is good that atleast we know what is happening in the background

FYI info I created a document called “doc.for_cwarn23”
That might give you an example for some suggestions to me

You have a lovely day
Joe Limboo

Isn't that basically the google syntax which will automatically be phrased when placed into a google search. If this is the case then those sorts of terms should already work or are you trying to add those types of phrasers to bing and yahoo?

bing and yahoo will not be able to take such long codes
only google can take

take a look at the exact match I looking for PDF and word docs

Here it goes
http://www.google.com/#hl=en&source=hp&q=php+~resume+(filetype%3Apdf+OR+filetype%3Adoc+OR+filetype%3Artf)+-jobs+-apply+-submit+-required+-wanted+-write+-sample&btnG=Google+Search&meta=&aq=f&oq=php+~resume+(filetype%3Apdf+OR+filetype%3Adoc+OR+filetype%3Artf)+-jobs+-apply+-submit+-required+-wanted+-write+-sample&fp=8ae7ca738c1cb608[/URL]

The above search is only word and pdf files

Why is the refine search adding up any reason
(intitle:resume OR inurl:resume) OR
This one adds up when clicked in the next page as

this the visitors keyword--> php (intitle:resume OR inurl:resume) OR
when clicked on the second page
php (intitle:resume OR inurl:resume) OR (intitle:resume OR inurl:resume) OR
3rd page...adds up three times not the visitors keyword, the refine search adds up go to 7th page it adds up 7 times

any advice
: )

Well then I will need to do some research on the bing+yahoo+google syntax to help you complete this one but because of a few other projects I'm doing it may take a little while. In the mean time you may keep on posting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.