I am building a site that parses a spanish dictionary. If you look up the word HOLA, you will receive the definition, but for other words, you get suggestions, like CASA: http://verbum.xtrweb.com/verbumpost.php?word0=hola&word-1=casa
I wish to: when you click on the suggestions (like CASAR in the example I posted above) to print the result in a div like HOLA. Here is the code I am currently using:
$words = array('word0','word-1');
function url_decode($string){
return urldecode(utf8_decode($string));
}
$baseUrl = 'http://lema.rae.es/drae/srv/search?val=';
$cssReplace = <<<EOT
<style type="text/css">
// I changed the style
</style>
</head>
EOT;
$resultIndex = 0;
foreach($words as $word) {
if(!isset($_REQUEST[$word]))
continue;
$contents = file_get_contents($baseUrl . urldecode(utf8_decode($_REQUEST[$word])));
$contents = str_replace('</head>', $cssReplace, $contents);
$contents = preg_replace('/(search?[\d\w]+)/','http://lema.rae.es/drae/srv/search', $contents);
echo "<div style='
//style
", (++$resultIndex) ,"'>", $contents,
"</div>";
}
I am starting to code, so please be patient, I have also tried with some DOM code a friend suggested but it failed to function.
i think you need to put an iframe inside div.
or try to make the click f'n happen in javascript/jquery - make some ajax call, upon click, and show/append the returned results (suggestions) inside the div
Related
how to check if text is present on a webpage using php and if true to execute some code?
My idea is to show some relevant products on the confirmation page after completing an order - if the name of the product is present on the page, then load some products. But I can't make the check for present text.
Case 1 if you prepare your page in a variable then echo it at the end of the script like
$response = "<html><body>";
$response .= "<div>contents text_to_find</div>";
$response .= "</body></html>";
echo $response;
then you can merely search the string with any string search function
if(strpos($response,"text_to_find") !==false){
//the page has the text , do what you want
}
Case 2 if you don't prepare the page in a string . and you just echo the contents and output the contents outside the <?php ?> tags like
<?php
//php stuff
?>
<HTML>
<body>
<?php
echo "<div>contents text_to_find</div>"
?>
</body>
</HTML>
Then you have no way to catch the text you want unless you use output buffering
Case 3 if you use output buffering - which I suggest - like
<?php
ob_start();
//php stuff
?>
<HTML>
<body>
<?php
echo "<div>contents text_to_find</div>"
?>
</body>
</HTML>
then you can search the output anytime you want
$response = ob_get_contents()
if(strpos($response,"text_to_find") !==false){
//the page has the text , do what you want
}
You may need to buffer your Output like so...
<?php
ob_start();
// ALL YOUR CODE HERE...
$output = ob_get_clean();
// CHECK FOR THE TEXT WITHIN THE $output.
if(stristr($output, $text)){
// LOGIC TO SHOW PRODUCTS WITH $text IN IT...
}
// FINAL RENDER:
echo $output;
Fastest solution is using php DOM parser:
$html = file_get_contents('http://domain.com/etc-etc-etc');
$dom = new DOMDocument;
$dom->loadHTML($html);
$divs = $dom->getElementsByTagName('div');
$txt = '';
foreach ($divs as $div) {
$txt .= $div->textContent;
}
This way, variable $txt would hold the text content of a given webpage, as long as it is enclosed around div tags, as usually. Good luck!
I am trying to get information using curl.
Approximately I got all information, but I need to get information individually.
For example I am getting a td text using curl.
here is the td content
jsfiddle
I need to extract text "my info", myinfo href link and last page number.
How i can do this?
here is my code which i am using in curl
$nodes = $finder->evaluate('//td[contains(text(), "") and starts-with(#id, "td_threadtitle_") ]');
foreach ($nodes as $node)
{
$innerHTML = trim($tmp_dom->saveHTML());
$fh = fopen("test.html", 'w'); // we create the file, notice the 'w'. This is to be able to write to the file once.
//writing response in newly created file
fwrite($fh, $node->c14n()); // here we write the data to the file.
fclose($fh);
}
My info
(//td[starts-with(#id, "td_threadtitle_") ]//a[1]/text())[1]
It's href
(//td[starts-with(#id, "td_threadtitle_") ]//a)[1]/#href
Last page number
substring-after(//td[starts-with(#id, "td_threadtitle_") ]//a[. = "Last Page"]/#href, "page=")
I tried this and it's according to my requirement.
Please tell me is this ok or not?
$options = $node->getElementsByTagName('a');
$post_message_id=$node->getAttribute('id');
foreach($options as $option) {
$value = $option->getAttribute('id');
if($value!=""){
print_r( $option->getAttribute('href'));
echo "\n";
print_r( $option->textContent);
echo "\n";
print_r($options->item(($options->length)-1)->getAttribute('href'));
echo "\n";
}
}
I have this issue with my jquery code:
<?php
//I oppen a text document, read the text inside of it and write it inside my html page.
//When someone clicks on a line, I want to take that very same line and send it via select(String) function.
$handle = fopen($_POST['lien'], 'r');
if ($handle)
{
while (!feof($handle))
{
$buffer = fgets($handle);
echo "<div onclick='select(\" ".$buffer." \");'>".$buffer."</div><br/>";
//It works when I put a simple string within the select param:
//echo "<div onclick='select(\" text \");'>".$buffer."</div><br/>";
}
fclose($handle);
}
?>
The jquery code :
function select(text){
alert(text);
//$("#selected").html();
}
where do you guys think is the problem ?
Thanks :)
Maybe Your $buffer contains some double-quotes, which then interfere with surrounding double-quotes. Look at produced HTML code if this is the case, maybe You'll see something like this:
<div onclick='select("He said: "Freeze!"")'>
... which Javascript can't parse correctly. If this is the case, consider using:
echo "<div onclick='select(".json_encode($buffer).")'>";
Try to change line:
echo "<div onclick='select(\" ".$buffer." \");'>".$buffer."</div><br/>";
to
echo "<div onclick='select(\" ".rtrim($buffer)." \");'>".$buffer."</div><br/>";
or to:
echo "<div onclick='select($(this).text());'>".$buffer."</div><br/>";
I am trying to parse html page of Google play and getting some information about apps. Simple-html-dom works perfect, but if page contains code without spaces, it completely ingnores attributes. For instance, I have html code:
<div class="doc-banner-icon"><img itemprop="image"src="https://lh5.ggpht.com/iRd4LyD13y5hdAkpGRSb0PWwFrfU8qfswGNY2wWYw9z9hcyYfhU9uVbmhJ1uqU7vbfw=w124"/></div>
As you can see, there is no any spaces between image and src, so simple-html-dom ignores src attribute and returns only <img itemprop="image">. If I add space, it works perfectly. To get this attribute I use the following code:
foreach($html->find('div.doc-banner-icon') as $e){
foreach($e->find('img') as $i){
$bannerIcon = $i->src;
}
}
My question is how to change this beautiful library to get full inner text of this div?
I just create function which adds neccessary spaces to content:
function placeNeccessarySpaces($contents){
$quotes = 0; $flag=false;
$newContents = '';
for($i=0; $i<strlen($contents); $i++){
$newContents.=$contents[$i];
if($contents[$i]=='"') $quotes++;
if($quotes%2==0){
if($contents[$i+1]!== ' ' && $flag==true) {
$newContents.=' ';
$flag=false;
}
}
else $flag=true;
}
return $newContents;
}
And then use it after file_get_contents function. So:
$contents = file_get_contents($url, $use_include_path, $context, $offset);
$contents = placeNeccessarySpaces($contents);
Hope it helps to someone else.
I am building a site-dictionary where you can search for more than one word at a time. I have a button to add inputs, one for each term. Now, i use those inputs and through a dictionary site (legally) i obtain their definitions and apply my own css style to them. So, when you type your word in input 1 (let's call it the like that) you get its definition in a div next to the input. So i have one variable to request the word, four others for the fetching and styling, an one last "echo" for the output. Here is the code:
enter code <?php
$data = preg_replace('/(search?[\d\w]+)/','http://lema.rae.es/drae/srv/\1', $data);
$word = $_REQUEST['word'];
$word2 = $_REQUEST['word2'];
$url = "http://lema.rae.es/drae/srv/search?val={$word}";
$url2 = "http://lema.rae.es/drae/srv/search?val={$word2}";
$css = <<<EOT
<style type="text/css">
</style>
EOT;
$data = file_get_contents($url);
$data2 = file_get_contents($url2);
$data = str_replace('<head>', $css.'</head>', $data);
$data2 = str_replace('<head>', $css.'</head>', $data2);
$data = str_replace('<span class="f"><b>.</b></span>', '', $data);
$data2 = str_replace('<span class="f"><b>.</b></span>', '', $data2);
echo '<div id="result1"
style="">
'.$data.'
</div>';
echo '<div id="result1"
style="">
'.$data2.'
</div>';
?>
Issue: how can i automatically generate this varibles (actually, the process itself) for every new input added?
Arrays are what you're looking for. Instead of creating a new variable $data{INDEX} you can create one variable that can hold a range of variables.
For example, if you want to 'push' an array with data you can do this.
$myData = array();
// appends the contents to the array
$myData[] = file_get_contents($url);
$myData[] = file_get_contents($url2);
Arrays allow for more versatility and efficiency.
You can find the documentation here.
A full implementation would look something like this.
// create an array of requests that we want
// to load in the url.
$words = array('word', 'word2');
// we'll use this later on for loading the files.
$baseUrl = 'http://lema.rae.es/drae/srv/search?val=';
// string to replace in the head.
$cssReplace = '<style type="text/css"></style></head>';
// string to remove in the document.
$spanRemove = '<span class="f"><b>.</b></span>';
// use for printing out the result ID.
$resultIndex = 0;
// loop through the words we defined above
// load the respective file, and print it out.
foreach($words as $word) {
// check if the request with
// the given word exists. If not,
// continue to the next word
if(!isset($_REQUEST[$word]))
continue;
// load the contents of the base url and requested word.
$contents = file_get_contents($baseUrl . $_REQUEST[$word]);
// replace the data defined above.
$contents = str_replace('</head>', $cssReplace, $contents);
$contents = str_replace($spanRemove, '', $contents);
// print out the result with the result index.
// ++$resultIndex simply returns the value of
// $resultIndex after adding one to it.
echo '<div id="result', (++$resultIndex) ,'">', $contents ,'</div>';
}