1

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 should look for API for what you trying to accomplish versus parsing out HTML. – Borik Apr 05 '13 at 02:52
  • Most simplest way would be, load the result in the div on the server call but hide it. Then change visibility depending upon click. You can use AJAX but it'l be overkill and definitely not needed here. – itachi Apr 05 '13 at 02:55

1 Answers1

1

to output content in a styled div, just replace:

echo "<div style='
      //style
     ", (++$resultIndex) ,"'>", $contents,
        "</div>";
}

with:

$divStyle = 'background: none repeat scroll 20% center transparent; margin-left: 275px; height: auto; box-shadow: 0px 0px 10px rgb(0, 0, 0) inset; border: 1px solid rgb(0, 0, 0); margin-top: 35px; padding-left: 14px; width: 793px; padding-bottom: 80px;';

//insert $divStyle as the "style" attribute
echo "<div style='" . $divStyle . "'" . (++$resultIndex) . "'>" . $contents . "</div>";

2 things changed:

  • dots, not commas to join stuff in PHP strings.
  • removed overflow(no need for the scrollbar) and added padding(extra internal space) to the divs bottom
mn.
  • 796
  • 6
  • 12
  • 1
    oh yeah forgot about that, make sure the HTML file has a .php extension lol. also make sure the php code is inside php tags, ie: ``. – mn. Apr 05 '13 at 02:58
  • 1
    sounds good! I will be off for the next 10 hours so i cant answer further questions you may have :). when it works, feel free to mark this question as answered. – mn. Apr 05 '13 at 03:02
  • Mike it isn't working so far... Creating a new file didnt work. I suspect it will if your answer is adapted in order to work all of it entirely in the code above. Probably changing suggestions for contents – La Pelota No Se Mancha Apr 07 '13 at 07:43
  • 1
    really? i will give you a live example on my site in a sec – mn. Apr 07 '13 at 07:49
  • Mike I appreciate your great effort, and I DID understand your answer. I would put it as correct but it really doesn't replace my previous comment. For me to fully explain myself please visit http://verbum.xtrweb.com/verbumpost.php?word0=hola&word-1=casa click on CASA and you will understand. I need what pops up in a div generated by the php code, just like in "hola". Dont forget the initial code I included above!!! – La Pelota No Se Mancha Apr 07 '13 at 08:54
  • 1
    ah k, sorry for an incorrect solution, will post another solution tomorrow morning. though what your wanting is [exactly like this yeah?](http://i.imgur.com/v3igMan.jpg). if so, the only code you needed to post above would be the last echo statement. – mn. Apr 07 '13 at 09:36
  • what I am looking for is EXACTLY the link you wrote. THAT's it! – La Pelota No Se Mancha Apr 07 '13 at 20:07
  • 1
    hey, good to know. see my edit, you will be able to know how to style the rest of the page using my new code. feel free to mark this question as answered, cheers. – mn. Apr 08 '13 at 01:14
  • Michael I am unfortunately stuck in the same problem. I want as a result the image with "casa" you posted two comments before. I am getting this with the code you kindly provided: https://twitter.com/_Verbum/status/321113886239637504/photo/1 Couldn't you send me the exact code you used for that image/example? – La Pelota No Se Mancha Apr 08 '13 at 04:18
  • sorry mike are you still available? – La Pelota No Se Mancha Apr 08 '13 at 21:42
  • 1
    Hey, yeah just been kinda busy lately. The image you just posted shows the correct css(as expected), so now just `echo` the content into this div (using the [exact same code used in the actual "casa" page](http://lema.rae.es/drae/srv/search?id=nmKVUynEDDXX2W8bh1gr|iYIQgBLPHDXX2vMtpp5k)) – mn. Apr 09 '13 at 00:26
  • Sorry to bother you again but what I am actually looking for is chaging this: www.twitter.com/_Verbum/status/321113886239637504/photo/1 to this: http://i.imgur.com/v3igMan.jpg – La Pelota No Se Mancha Apr 09 '13 at 00:56
  • 1
    Yep, the method to set the CSS above + echo contents into div(explained in my first post, and the live example) can achieve that :) – mn. Apr 09 '13 at 06:01
  • can we chat or something? There is something I can't figure out – La Pelota No Se Mancha Apr 09 '13 at 22:57