I'm making a search engine, and one of the features will be that it searches Google, among other things. The first thing I did was find out how to generate a Google search based on URL alone. Well, "http://www.google.com/search?hl=en&source=hp&biw=1542&bih=928&q=" is the first part, "Query" is the second part, and "&aq=f&aqi=g10&aql=&oq=&qscrl=1" is the third part. Now, on the client-side I made a form I called "search", and the action was the file below. However, my attempts to make this plausible on server-side have failed completely. Below is the code:
<?php
$something = $_REQUEST["search"];
$txt1="http://www.google.com/search?hl=en&source=hp&biw=1542&bih=928&q=";
$txt2 = $something;
$txt3="&aq=f&aqi=g10&aql=&oq=&qscrl=1";
$string=$txt1.$txt2."+".$txt3;
$link_to_dig = $string;
$original_file = @file_get_contents($link_to_dig);
if(!$original_file)
die("Error loading {$link_to_dig}");
$path_info = parse_url($link_to_dig);
$base = $path_info['scheme'] . "://" . $path_info['host'];
$stripped_file = strip_tags($original_file, "<a>");
$fixed_file = preg_replace("/<a([^>]*)href=\"\//is", "<a$1href=\"{$base}/", $stripped_file);
$fixed_file = preg_replace("/<a([^>]*)href=\"\?/is", "<a$1href=\"{$link_to_dig}/?", $fixed_file);
preg_match_all("/<a(?:[^>]*)href=\"([^\"]*)\"(?:[^>]*)>(?:[^<]*)<\/a>/is", $fixed_file, $matches);
//DEBUGGING
//$matches[0] now contains the complete A tags; ex: <a href="link">text</a>
//$matches[1] now contains only the HREFs in the A tags; ex: link
$result = print_r($matches, true);
$result = str_replace("<", "<", $result);
print "<pre>" . $result . "</pre>";
?>
Thanks for helping!
This is the form:
<form class="searchengine" name="search" action="search.php" method="post">
<input type="text" style="width:580px; height:35px; color:#000000;" class="search" name="search" />
</form>
I'm assuming you're very new to php and have gotten this script from somewhere and trying to modify it to fit.
echo - Output one or more strings - No value is returned.
So:
$txt2 = echo "$something"
First off it fails because there is no ending semicolon.
Is printing $something to the screen, and $txt is being set to nothing.
You do already have the value in $something, so I don't know why you'd want to change it to another variable, but this will do it:
$txt2 = $something;
Then if you want to make the script nicer, the majority of people on SO will point you towards DOMDocument .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.