Convert PHP snippet to JavaScript

Discussion in 'JavaScript' started by BRUm, Sep 29, 2006.

  1. #1
    Hi,

    If there is anyone here who's familiar with PHP and Javascript, I'd be greatly thankful if you could convert this snippet of PHP to JavaScript please. I'm very good with PHP, but I know nothing about JavaScript. So I need to offer clients who don't have PHP websites the ability to use my code via JavaScript.

    <!--- Beginning of CPCads code --->
    
    <? $content = file_get_contents('http://www.cpcads.net/cgi.php?cat=advertising'); 
    preg_match_all('/(<cat>)(.+?)(<\/cat>)/i', $content, $title); 
    
    $count = count($title);
    $x = 0;
    
    WHILE($x < $count){
    echo $title[0][$x]."<br>"; 
    $x++;
    }
    
    //echo "<br><br>".print_r($title);
    
    ?>
    
    <!--- End of CPCads code --->
    Code (markup):
    Thank you,

    Lee.
     
    BRUm, Sep 29, 2006 IP
  2. penagate

    penagate Guest

    Messages:
    277
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Hi!

    You have to remember firstly that Javascript is not an alternative to PHP. PHP is a server-side language, Javascript is purely client-side. One cannot replace the other.

    Having said that, you can convert that sample into usable JS. But, it will differ from a server-side implementation in that its behaviour will be completely dependent upon the user agent and its capability to execute Javascript.

    I assume you maintain cpcads.net. Make a new PHP script that returns the adverts in XML format, and you can use the XMLHttpRequest object to obtain them through Javascript. I can write a sample of that in a sec if you like.

    Regards
    - P
     
    penagate, Sep 29, 2006 IP
  3. BRUm

    BRUm Well-Known Member

    Messages:
    3,086
    Likes Received:
    61
    Best Answers:
    1
    Trophy Points:
    100
    #3
    Hi,

    Thanks for your comment. But, this script acts clientside. It does not make any configurations or modifications. I just need to know how to get this script into JS as googleadsense has a JS version. All this script does, is regex grab a page! And I know JS can do this, so it is possible. Would you be able to grab the page contents of: http://www.cpcads.net/cgi.php?cat=advertising ?

    And Yes, I own cpcads.net :)

    Thanks,

    Lee.
     
    BRUm, Sep 29, 2006 IP