Advertising (This ad goes away for registered users. You can Login or Register)

PHP or Javascript?

Programming on your favorite platform, for your favorite platform? Post here
Post Reply
User avatar
cmbeke
Posts: 50
Joined: Tue Apr 12, 2011 9:51 pm

PHP or Javascript?

Post by cmbeke » Wed Jan 11, 2012 1:27 am

I'll looking to make a something that will take a webpage and find links in it and don't know what to use, so I thought I'd ask here. Is that even possible with PHP or Javascript? What do you guys recommend?
Advertising
You probably didn't know that you don't know that you don't know what you don't know.
I know I'm crazy, the real question is are you?
Life is nothing but discrimination, likes, dislikes, any choice made ... all a product of discrimination.

User avatar
ohrores
Posts: 21
Joined: Thu Apr 14, 2011 5:11 pm
Location: Germany

Re: PHP or Javascript?

Post by ohrores » Wed Jan 11, 2012 4:17 pm

I think it would be hard with both.
Because Javascript is a client-side language and PHP a server-side language.
That means PHP runs as/on the server and Javascript on the PC of the client,
interpreted by the browser (as example Firefox).
I do not know Javascript nor PHP, so I am not sure.
I suppose a scripting language would be the best choice.
Advertising
[spoiler]Image[/spoiler]

User avatar
m0skit0
Guru
Posts: 3817
Joined: Mon Sep 27, 2010 6:01 pm

Re: PHP or Javascript?

Post by m0skit0 » Wed Jan 11, 2012 11:36 pm

ohrores wrote:I suppose a scripting language would be the best choice.
Perl FTW. Does wonders with file parsing. Wanna me show you how to do a web page parser?

Also a correction if you don't mind:
ohrores wrote:Because Javascript is a client-side language and PHP a server-side language.
That means PHP runs as/on the server and Javascript on the PC of the client,
Yes you're right, but you can anyway get a PHP and/or JS interpreter and run code on your own PC ;) But as I said above, Perl also excels at this.

EDIT: Just for fun, I made it. It was a nice exercise. Probably needs a bit of polishing but it does extract the links from web pages ;)

Code: Select all

#!/usr/bin/perl
# Online HTML Parser by m0skit0
# 12/01/2012
# Usage: script.pl <URL to parse>

use strict;
use HTML::Parser;
use LWP::UserAgent;

use constant HTTP_AGENT => "DarthPerl/3.0";
use constant HTML_LINK_TAG => "a";

# Invoked each time a starting tag is found
sub start
{
	my ($tagname, $attr) = @_;
	if ($tagname eq HTML_LINK_TAG)
	{
		print ("$attr->{href}\n");
	}
}

########
# MAIN #
########

# Create a UserAgent (HTTP client)
my $ua = LWP::UserAgent->new();
$ua->agent(HTTP_AGENT);

# Create an HTTP request
my $req = HTTP::Request->new(GET => $ARGV[0]);

# Request the page
my $res = $ua->request($req);

# If successful
if ($res->is_success)
{
	# Create an HTML parser
	my $parser = HTML::Parser->new
	(
		api_version => 3,
		start_h => [\&start, "tagname, attr"],
	);
	
	# Parse web page
	$parser->parse($res->content);
}

# Request failed
else
{
	print "$res->status_line\n";
}

# Bye bye and have fun
I wanna lots of mov al,0xb
Image
"just not into this RA stuffz"

User avatar
cmbeke
Posts: 50
Joined: Tue Apr 12, 2011 9:51 pm

Re: PHP or Javascript?

Post by cmbeke » Thu Jan 12, 2012 5:02 pm

thanks for the example :D i'll guess i'll try and learn perl then. hoping the make something to help me with my webcomic addiction (have over 100 bookmarked that I check each day :oops: )
You probably didn't know that you don't know that you don't know what you don't know.
I know I'm crazy, the real question is are you?
Life is nothing but discrimination, likes, dislikes, any choice made ... all a product of discrimination.

Post Reply

Return to “Programming”