PDA

View Full Version : PHP Project Help Needed



Moonbat
03-27-2008, 06:54 PM
Alright, well, I'm working on a project, here is the gist of what it is. I got the idea from another site.

When someone is reading PHP code, they might wonder what a certain function, like 'explode' or 'nl2br' does. They would probably Google it up. But this makes the process easier. What you do is enter your PHP code into the text box, and then when you submit it, it'll output the PHP code, but with many important statements and functions already hyperlinked to the PHP.net function reference. It may not be particularly useful, but it's my first short-term project.

Sadly, I've come accross my first hurdle. Here is the full code:

<html>
<head>
<title>PHP Keyword Parser v0.*</title>
<style type="text/css">
textarea
{
background: black;
color: #FFFFFF;
}
a:link
{
color: #FFFFFF;
font-weight: bold;
}
a:visited
{
color: #FFFFFF;
font-weight: bold;
}
pre
{
color: white;
}
</style>
</head>
<body bgcolor="666666">

<?php
if (isset($_POST['thephpcode'])){
$thephpcode = $_POST['thephpcode'];
$thekeywords = array("echo", "print", "fsockopen");
$thelinkarrays = array("<a href='http://www.php.net/echo'>echo</a>", "<a href='http://www.php.net/print'>print</a>",
"<a href='http://www.php.net/fsockopen'>fsockopen</a>");
str_replace($thekeywords, $thelinkarrays, $thephpcode);
print "<pre>$thephpcode</pre>";
} ;
?>

<center><font color="0066FF" face="Verdana"><h*>PHP Keyword Parser v0.*</h*></font></center>
<center><font face ="Verdana">Enter your PHP code into the text box below, and then press 'Parse'</font></center>
<br>
<form name="thephpcodeform" action="<?php echo $PHP_SELF; ?>" method="POST">
<center><textarea name="thephpcode" rows="22" cols="*00%"></textarea><center>
<input type="submit" value="Parse">
</form>
</body>
</html>
I get no errors (and I haven't done anything to suppress errors, such as @, so I should get errors if there are any because I've gotten them before).

But the problem is when I enter in the code, it just echoes back the code instead of replacing the specified keywords with the linked versions. I've checked the code >*000 times and I can't figure out why it isn't working. I'm testing this on AwardSpace webhost, but I don't think that makes any difference.

If you can help, I'd appreciate it.:D

coz
03-27-2008, 07:35 PM
I'm don't know much about php and should even be saying anything but it looks like you never told it to do anything to the entered text. You just put a bunch of keywords and things in variables/arrays but none of them ever get worked on (ran through a loop or string comparison or something).

Moonbat
03-27-2008, 07:55 PM
Actually, it does do something :P I'll explain it:

if (isset($_POST['thephpcode'])){
This line checks to see if there is any POST data from the form 'thephpcodeform' with the name 'thephpcode'.

$thephpcode = $_POST['thephpcode'];
$thekeywords = array("echo", "print", "fsockopen");
$thelinkarrays = array("<a href='http://www.php.net/echo'>echo</a>", "<a href='http://www.php.net/print'>print</a>", "<a href='http://www.php.net/fsockopen'>fsockopen</a>");
This does three things.

Assigns the POST data from the 'thephpcodeform' form to a stable variable
Assigns the array of keywords echo, print, and fsockopen to a variable
Assigns an array of the hyperlinked versions of the keywords to a variable


str_replace($thekeywords, $thelinkarrays, $thephpcode);
print "<pre>$thephpcode</pre>";
} ;
This (is supposed to) search for the keywords the variable $thekeywords holds in the string of PHP code named $thephpcode. It will replace those keywords with the ones in the variable $thelinkarrays. But that's not happening :(

coz
03-27-2008, 09:27 PM
It looks to me like the str_replace function is the only thing that may attempt to do something but it doesn't return a value to do it. Maybe it doesn't have to to use the function? Just trying to help man.

Moonbat
03-27-2008, 10:00 PM
It looks to me like the str_replace function is the only thing that may attempt to do something but it doesn't return a value to do it. Maybe it doesn't have to to use the function? Just trying to help man.
I feel really stupid now :(

I realized that I forgot to assign the result of the str_replace() function to a variable.

But now that I've realized it (thanks to coz), my script finally works! All I gotta do is fill in the rest of the keywords :D

Muchas gracias coz, don't know what I'd do without you :p

Oh, BTW coz, here is your prize... (http://www.encyclopediadramatica.com/images/4/47/You_win_the_prize.jpg)

JayT
03-27-2008, 10:58 PM
Not sure I fully understand the idea. Do you mean something like the following?

It's built around a simple function to convert a PHP function keyword into an HTML hyperlink string to the corresponding PHP manual page.

It wouldn't be too difficult to create a button to go directly to the PHP manual when clicked.

To demonstrate the function, the generated link is displayed on the page.



<?PHP

$keyword = @$_POST['keyword'];

$link = Keyword_To_Link ($keyword);


print <<< _HTML
<!DOCTYPE HTML PUBLIC "-//W*C//DTD HTML 4.0* Transitional//EN"
"http://www.w*.org/TR/html4/loose.dtd">

<HTML>
<HEAD>

<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-885*-*">

<TITLE>Lookup PHP Keyword</TITLE>

</HEAD>

<BODY>

<FORM NAME="KeywordForm" METHOD="post" ACTION="kw.php">

<TABLE WIDTH="264" BORDER="*">

<TR><TD COLSPAN="2" ALIGN="CENTER"><B>Enter PHP Function Keyword</B></TD></TR>

<TR>
<TD WIDTH="**2"><INPUT NAME="keyword" TYPE="text" SIZE="*2" MAXLENGTH="*2" VALUE="$keyword"></TD>
<TD WIDTH="56"><INPUT TYPE="submit" NAME="Submit" VALUE="Make Link"></TD>
</TR>

</TABLE>

<P>$link</P>

</FORM>

</BODY>
</HTML>

_HTML;


function Keyword_To_Link ($KeywordArg)

{
$BaseURL = "http://us2.php.net/manual/en/";
$keyword = StrToLower(trim($KeywordArg));
$HLink = "<A HREF=\"$BaseURL" . "function.$keyword.php\">$keyword</A>";

return $HLink;
}


?>

Moonbat
03-28-2008, 05:35 PM
Not exactly JayT. Your code will only take one keyword and produce a link to it. What I aim to do is let the user enter a bunch of PHP code (wthout the beginning and ending tags). Once entered, my script will go through and look for a large list of keywords, and if it finds any, it'll replace them with a link to the database. For example, my script will turn:

echo "hai";
fsockopen(insertrandomcarajflkfjkasljfopwopa);
print "y halo thar?";
$var ajflka = array("faksjflk", "kfajslfk", "kdfjasld");
Into:

echo (http://php.net/echo) "hai";
fsockopen (http://php.net/fsockopen)(insertrandomcarajflkfjkasljfopwopa);
print (http://php.net/print) "y halo thar?";
$var ajflka = array (http://php.net/array)("faksjflk", "kfajslfk", "kdfjasld");
Right now I've got it working, just working on the formatting and look of the whole thing.

coz
03-28-2008, 05:40 PM
What can I say, It's great to be the man.:cool: That's one funny ass prize man.:D

JayT
03-29-2008, 10:32 AM
Not exactly JayT. Your code will only take one keyword and produce a link to it.



Yes, I know. That was the idea. It was to demonstrate the core concept.

I thought a simple function to convert a keyword to a link might be called from within a loop to handle that single task in a independent modular fashion. Then you could cycle through the array calling the function for each keyword.

I built a huge library of custom functions and use them to create programs without reinventing the wheel each time.

The keyword to link function was one of many.

My usual style is to create modular, compatible building block functions that I can assemble and combine as needed like leggos to build projects.

It's like extending the PHP language and adding your own new custom functions.


Saves me work.

:)