Jump to content


Photo

Is this php code right ? asp.net to php conversion


  • Please log in to reply
5 replies to this topic

#1 rabid lemming

rabid lemming
  • New Members
  • Pip
  • Newbie
  • 1 posts

Posted 22 July 2006 - 02:25 PM

Hey ya all  ;D

I am trying to convert to following asp.net code to php version.

My php code which always says there is a phase error on line 2 "Parse error: parse error in c:\program files\easyphp1-8\www\index.php on line 2" even if I remove line 2 and 16, 17 & 18

<?php
try {
	$GetTheRegularExpressionString = $_POST['TheRegularExpression'];
	$GetTheStringToValidateString = $_POST['TheString'];
	if ((isset($GetTheRegularExpressionString) == true) && (isset($GetTheStringToValidateString) == true)) {
		if (preg_match($GetTheRegularExpressionString, $GetTheStringToValidateString) == true) {
			print "dat=true";
		} else {
			print "dat=false";
		}
	} else {
		print "dat=opps";
		$URL="RegularExpressionsHTMLVersion.aspx";
		header ("Location: $URL");
	}
} catch (Exception $exception) {
	print ("dat=" + $exception->getMessage()."\n".$error->getDebugInfo()); 
}
?>

My asp.net code:

<%@ Import NameSpace="System" %>
<%@ Import NameSpace="System.Net" %>
<%@ Import NameSpace="System.IO" %>
<%@ Import NameSpace="System.Text.RegularExpressions" %>
<%@ Page Language="VB" Debug="true" validateRequest="false" AutoEventWireup="true" %>
<script runat="server"> 
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        ' check page isn't been post back from form event procedures
        Try
            Dim GetTheRegularExpressionString As String = New String(Request.Form("TheRegularExpression"))
            Dim GetTheStringToValidateString As String = New String(Request.Form("TheString"))
            If GetTheRegularExpressionString.Length <> 0 And GetTheStringToValidateString.Length <> 0 Then
                Dim StringRegex As System.Text.RegularExpressions.Regex = New System.Text.RegularExpressions.Regex(GetTheRegularExpressionString)
                If StringRegex.Match(GetTheStringToValidateString).Success = True Then
                    Response.Write("dat=True")
                Else
                    Response.Write("dat=False")
                End If
            Else
                Response.Write("dat=False")
                Response.Redirect("RegularExpressionsHTMLVersion.aspx")
            End If
        Catch ex As Exception
            Response.Write("dat=" & ex.ToString)
        End Try
    End Sub
</script>

Can any one tell me what I’m missing as far as i can see it all seams ok !?  ???

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 22 July 2006 - 02:27 PM

Is 'try' ment to be a function? if so then you need this

<?php
function try() {
	$GetTheRegularExpressionString = $_POST['TheRegularExpression'];
	$GetTheStringToValidateString = $_POST['TheString'];
	if ((isset($GetTheRegularExpressionString) == true) && (isset($GetTheStringToValidateString) == true)) {
		if (preg_match($GetTheRegularExpressionString, $GetTheStringToValidateString) == true) {
			print "dat=true";
		} else {
			print "dat=false";
		}
	} else {
		print "dat=opps";
		$URL="RegularExpressionsHTMLVersion.aspx";
		header ("Location: $URL");
	}
} catch (Exception $exception) {
	print ("dat=" + $exception->getMessage()."\n".$error->getDebugInfo()); 
}
?>

www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 Joe Haley

Joe Haley
  • Members
  • PipPipPip
  • Advanced Member
  • 103 posts
  • LocationCanada, eh?

Posted 22 July 2006 - 02:31 PM

Is 'try' ment to be a function? if so then you need this


you are WAY off.

http://php.net/manua....exceptions.php

You need PHP 5 to use try / catch blocks and exceptions.
Give a man a fish; you have fed him for today.  Teach a man to fish; and you have fed him for a lifetime
Don't teach men to program. Teach them to fish.

Please, try the RTFM solution before asking for help:
http://php.net/manual/en/index.php

#4 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 22 July 2006 - 02:36 PM

ahh ok sorry lol never ever seen anyone use 'try' in there code  :-[ im on PHP 4 not 5 also so not come across it..

Will leave you in peace now lol

Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 22 July 2006 - 03:37 PM

Change:
preg_match($GetTheRegularExpressionString, $GetTheStringToValidateString) == true
To just:
$GetTheRegularExpressionString == $GetTheStringToValidateString

Also you cannot use header when you output something to the browser, here:
print "dat=opps"; // connot oputput this, before you use header!
$URL="RegularExpressionsHTMLVersion.aspx";
header ("Location: $URL");


#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,019 posts

Posted 22 July 2006 - 06:03 PM

$GetTheRegularExpressionString = $_POST['TheRegularExpression'];
$GetTheStringToValidateString = $_POST['TheString'];
if ((isset($GetTheRegularExpressionString) == true) && (isset($GetTheStringToValidateString) == true)) {


You cannot use isset() here as it will always return true, since the variables were set on the 2 previous lines.

Use

if (!empty($GetTheRegularExpressionString) && !empty($GetTheStringToValidateString) ) {

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users