Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi can anyone do this favor and translate this code to c# vb.net or javascript any translation i would be grateful for
PHP
<?php
$url = trim($_REQUEST['url']);
$url = check_url($url);

function check_url($value)
{
    $value = trim($value);
    if (get_magic_quotes_gpc())
    {
        $value = stripslashes($value);
    }
    $value = strtr($value, array_flip(get_html_translation_table(HTML_ENTITIES)));
    $value = strip_tags($value);
    $value = htmlspecialchars($value);
    return $value;
}

function file_get_contents_curl($url)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $data = curl_exec($ch);
    $info = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);

    //checking mime types
    if(strstr($info,'text/html')) {
        curl_close($ch);
        return $data;
    } else {
        return false;
    }
}

//fetching url data via curl
$html = file_get_contents_curl($url);

if($html) {
//parsing begins here:
$doc = new DOMDocument();
@$doc->loadHTML($html);
$nodes = $doc->getElementsByTagName('title');

//get and display what you need:
$title = $nodes->item(0)->nodeValue;
$metas = $doc->getElementsByTagName('meta');

for ($i = 0; $i < $metas->length; $i++)
{
    $meta = $metas->item($i);
    if($meta->getAttribute('name') == 'description')
        $description = $meta->getAttribute('content');
}

// fetch images
$image_regex = '/<img[^>]*'.'src=[\"|\'](.*)[\"|\']/Ui';
preg_match_all($image_regex, $html, $img, PREG_PATTERN_ORDER);
$images_array = $img[1];
?>
<div class="images">
    <?php
    $k=1;
    for ($i=0;$i<=sizeof($images_array);$i++)
    {
        if($images_array[$i])
        {
            if(strstr($images_array[$i],'http')) {
                echo "<img src='".$images_array[$i]."' width='100' id='".$k."' >";
                $k++;
            }
        }
    }
    ?>
    &lt;input type="hidden" name="total_images" id="total_images" value="<?php echo --$k?>" />
    </div>
    <div class="info">

        &lt;label class="title">
            <?php  echo $title; ?>
        &lt;/label>
        <br clear="all" />
        &lt;label class="url">
            <?php  echo substr($url ,0,35); ?>
        &lt;/label>
        <br clear="all" /><br clear="all" />
        &lt;label class="desc">
            <?php  echo $description; ?>
        &lt;/label>
        <br clear="all" /><br clear="all" />

        &lt;label style="float:left"><img src="prev.png" id="prev" alt="" /><img src="next.png" id="next" alt="" />&lt;/label>

        &lt;label class="totalimg">
            Total <?php echo $k?> images
        &lt;/label>
        <br clear="all" />

    </div>
<?php
} else {
echo "Please enter a valid url";
}
?>
Posted
Updated 5-Jun-12 11:26am
v2

 
Share this answer
 
Comments
VJ Reddy 5-Jun-12 20:35pm    
Good reference to several conversion tools. 5!
thatraja 5-Jun-12 21:24pm    
I'm going to update that blog post quickly, if possible give me some feedback.
Sergey Alexandrovich Kryukov 5-Jun-12 23:31pm    
Just voted 5 for that article and this answer.
Unfortunately, this is not what OP really wants; please see my comment to the answer by Tim.
--SA
codeBegin 6-Jun-12 7:07am    
my 5
Translating code from one language to another is not really a good idea. Different languages have different optimizations and ways of doing things. Since you are looking for this to be translated into any other language, I think it would be better for you to take what the code is doing (conceptually) and figure out how to do it in the language of your choice.

As a last resort, you could use this code directly inside .NET as evidenced by the second most popular answer to this question on Stack Overflow:

http://stackoverflow.com/questions/5221669/php-to-c-sharp-converter[^]

As for what this specific code does, in case you are wondering, it loads a webpage into memory and then parses it. Basically, it looks like you are doing screen scraping or other such tasks. There are demos for how to do this in each of the languages you mentioned above.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Jun-12 17:38pm    
My 5. I agree with you -- this is a bad idea, and the converter you advised could really be a last resort.
However, I used a very different, better option -- PHP for .NET; please see my answer.
--SA
Sergey Alexandrovich Kryukov 5-Jun-12 17:49pm    
[This is the OP's reply; I moved it from the "answer":]

hi everyone thank you for trying to help i would translate the code but i know nothing in php anw i found the same output i need done in javascript Smile | :)
Sergey Alexandrovich Kryukov 5-Jun-12 17:53pm    
You should started from this explanation! Why would you waste time on answering just because you did not express yourself in a comprehensive manner?
Now, it looks like you do not know what do you want. JavaScript cannot be an alternative to PHP, and not visa versa. PHP works only on server side, and JavaScript on client side.
(There is server-side JavaScript, but it's rare, and I don't think you mean it.) By the same reason, ".NET or JavaScript" are also not alternatives.
By the same reason,

Do you clearly understand the difference?! Your whole approach is wrong: you should not switch technologies or translate something, you should learn what technology is the best for some purpose and learn it. Learning is much better then adapting anything to anything, more effective and satisfying.
--SA
VJ Reddy 5-Jun-12 20:38pm    
Good answer. 5!
thatraja 5-Jun-12 21:42pm    
5!
I agree with Tim Corey that this is not a good idea, but there is one more interesting option: using PHP for .NET. Such product does exist, it it's open-source:
http://en.wikipedia.org/wiki/Phalanger_%28compiler%29[^],
http://www.php-compiler.net/[^].

I used it to develop regular PHP code (to be able to use the debugger) and was quite satisfied.

—SA
 
Share this answer
 
Comments
VJ Reddy 5-Jun-12 20:35pm    
Good answer. 5!
Sergey Alexandrovich Kryukov 5-Jun-12 20:37pm    
Thank you, VJ.
--SA
thatraja 5-Jun-12 21:43pm    
5!
I think I have to update my blogpost with this one.
Sergey Alexandrovich Kryukov 5-Jun-12 23:27pm    
Thank you, Raja. Did you try it?
--SA
codeBegin 6-Jun-12 7:07am    
my 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900