lynx
: A Text Browser (Cont.)
The following interface demonstrates how to use lynx
in PHP scripts.
This interface searches the URL content for the lines containing the keyword.
☂You may need to create the empty file result.txt
initially, and open it via
shell> chmod 777 result.txt
Otherwise, the file result.txt
may not be written.
Displaying the Lines in the URL Content Containing the Keyword
|
|
http://undcemcs01.und.edu/~wen.chen.hu/course/525/2/Lynx.php
|
|
<?php
# Remove leading and trailing spacing.
$URL = trim( $_POST["URL"] );
$keyword = trim( $_POST["keyword"] );
# For security, remove some Unix metacharacters.
$meta = array( ";", ">", ">>", ";", "*", "?", "&", "|" );
$URL = str_replace( $meta, "", $URL );
$keyword = str_replace( $meta, "", $keyword );
if ( $_POST['act'] == "Search the URL content" ) {
header( "Content-type: text/plain" );
$cmd = "lynx -dump -source '" . $URL . "' > result.txt";
echo ( $cmd . "\n\n" );
system( "chmod 777 result.txt ../2/" );
system( $cmd );
system( "chmod 755 result.txt ../2/" );
$cmd = "grep $keyword result.txt";
echo ( $cmd . "\n\n\n" );
system( $cmd );
}
elseif ( $_POST["act"] == "Display the URL content" ) {
header( "Content-type: text/plain" );
$cmd = "lynx -dump -source '" . $URL . "' > result.txt";
echo ( $cmd . "\n\n\n" );
system( "chmod 777 result.txt ../2/" );
system( $cmd );
system( "chmod 755 result.txt ../2/" );
system( "cat result.txt" );
}
elseif ( $_POST["act"] == "Help" ) {
header( "Content-type: text/html" );
$file = fopen( "Help.html", "r" ) or
exit( "Unable to open file!" );
while ( !feof( $file ) )
echo fgets( $file );
fclose( $file );
}
else {
header( "Content-type: text/html" );
echo( "<html><body>" );
echo( "<h3>No such option: " . $_POST["act"] . "" );
echo( "</body></html>" );
}
?>
|
The following PHP and Unix functions are used:
- PHP
trim(string)
:
Removes whitespace or other characters from both sides of a string.
- PHP
str_replace(find,replace,string)
:
Replaces some characters in a string (case-sensitive).
- PHP
system(command)
:
Executes an external program and display the output.
- Unix
chmod MODE FILE
:
Changes the permission of a file.
- Unix
grep PATTERN [FILE]
:
Finds text within a file.
“We must live together as brothers or perish together as fools.”
― Martin Luther King Jr.
|