Jump to content

preg_match() returning 0


Love2c0de

Recommended Posts

Good morning,

 

I have a script and I'm trying to check whether it contains 1 character and whether it is between A and Z.

 

preg_match() seems to be returning 0 and I'm not sure why.

 

Here is my code:

 

<?php



$conn = mysqli_connect("localhost","root","","gaming") or die("Error: ".mysqli_error());



$qry = "SELECT game_id, release_date, game_desc, game_icon FROM games";



if(isset($_GET['letter']))

{    

    /*if(strlen($_GET['letter']) == 1)

    {

        

    }

    else

    {

        header("Location: ?page=games");

    }*/

    $match = preg_match("/A-Z/", $_GET['letter']);

    echo $match;



    $qry .= " WHERE game_tag = '{$_GET['letter']}'";

}



$sql = mysqli_query($conn, $qry) or die("Error: ".mysqli_error($conn));



$game_output = "";



while($row = mysqli_fetch_assoc($sql))

{        

    $game_output .= "<div class='game_containers'>";

    $game_output .= "<img src='images/game_icons/{$row['game_icon']}' alt='{$row['game_id']}' class='game_icon' />";

    $game_output .= "<h3 class='game_header'>{$row['game_id']}</h3>";

    $game_output .= "<p class='game_desc'>{$row['game_desc']}</p>";

    $game_output .= "<hr />";

    $game_output .= "Release Date: <span class='game_date'>{$row['release_date']}</span>";

    $game_output .= "</div>";

}





mysqli_close($conn);

?>

 

When I echo $match, it always returns 0 even though $_GET['letter'] contains say 'M' or 'C'

 

Any thoughts?

 

Kind regards,

 

L2c.

Link to comment
https://forums.phpfreaks.com/topic/275452-preg_match-returning-0/
Share on other sites

Good evening Salathe,

 

That's correct, I've setup a CHAR table field to cater for tihs value. I think my code should work now because I check the length of $_GET['letter'] and make sure it's equal to 1 before performing the preg_match().

 

Here is my completed code:

 

<?php

$conn = mysqli_connect("localhost","root","","gaming") or die("Error: ".mysqli_error());

$qry = "SELECT game_id, release_date, game_desc, game_icon FROM games";

if(isset($_GET['letter']))
{    
    if(strlen($_GET['letter']) == 1)
    {
        if(preg_match("/[A-Z]/", $_GET['letter']) === 1)
        {
            $qry .= " WHERE game_tag = '{$_GET['letter']}'";
        }
        else
        {
            header("Location: ?page=games");
        }
    }
    else
    {
        header("Location: ?page=games");
    }    
}

$sql = mysqli_query($conn, $qry) or die("Error: ".mysqli_error($conn));

$game_output = "";

while($row = mysqli_fetch_assoc($sql))
{        
    $game_output .= "<div class='game_containers'>";
    $game_output .= "<img src='images/game_icons/{$row['game_icon']}' alt='{$row['game_id']}' class='game_icon' />";
    $game_output .= "<h3 class='game_header'>{$row['game_id']}</h3>";
    $game_output .= "<p class='game_desc'>{$row['game_desc']}</p>";
    $game_output .= "<hr />";
    $game_output .= "Release Date: <span class='game_date'>{$row['release_date']}</span>";
    $game_output .= "</div>";
}


mysqli_close($conn);
?>

 

I'm always open to suggestions on how to do things better though. I had the length in mind when I decided to go for regexp but didn't know how to edit the regular expression so I used strlen() instead.

 

Kind regards,

 

L2c.

Do the length check in the RegExp as well, to avoid unnecessary code and (not to mention) nesting.

 

Also, I would recommend that you reverse the logic of your test a bit. So that you're checking for error conditions instead. This will not only make it a lot easier to read your code, as your error messages will immediately follow the test which produces them; But it will also help reduce the amount of nesting required, and thus further improve the readability.

Lastly: Always use die after a header redirect.

Archived

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

×
×
  • Create New...

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.