Jump to content

Archived

This topic is now archived and is closed to further replies.

rabid lemming

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

Recommended Posts

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

[code]
<?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());
}
?>
[/code]

My asp.net code:

[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>[/code]

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

Share this post


Link to post
Share on other sites
Is 'try' ment to be a function? if so then you need this

[code]<?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());
}
?>[/code]

Share this post


Link to post
Share on other sites
[quote author=shocker-z link=topic=101472.msg401664#msg401664 date=1153578466]
Is 'try' ment to be a function? if so then you need this
[/quote]

you are WAY off.

http://php.net/manual/en/language.exceptions.php

You need PHP 5 to use try / catch blocks and exceptions.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Change:
[code]preg_match($GetTheRegularExpressionString, $GetTheStringToValidateString) == true[/code]
To just:
[code]$GetTheRegularExpressionString == $GetTheStringToValidateString[/code]

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

Share this post


Link to post
Share on other sites
[quote]$GetTheRegularExpressionString = $_POST['TheRegularExpression'];
$GetTheStringToValidateString = $_POST['TheString'];
if ((isset($GetTheRegularExpressionString) == true) && (isset($GetTheStringToValidateString) == true)) {[/quote]

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

Use

[code]if (!empty($GetTheRegularExpressionString) && !empty($GetTheStringToValidateString) ) {[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.