Solved

How to accept the \ "backslash" with the preg_match

Posted on 2006-11-16
5
1,174 Views
Last Modified: 2013-12-13

Hello, I'm having a problem with accepting the \ for my session.
Please show me how to accept the \ with preg_match function.



if (!isset($_SESSION)) session_start();
$_SESSION['Test']="sds \'";
if(!preg_match("/^[a-zA-Z0-9 \-\ ]{1,45}$/", stripslashes($_SESSION['Test'])) == 1){
echo "error<br>";
echo $_SESSION['Test'];
}
0
Comment
Question by:MaxDisk
  • 2
  • 2
5 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 17963158
this should do:
if(!preg_match("/^[a-zA-Z0-9 \-\ \\]{1,45}$/", stripslashes($_SESSION['Test'])) == 1){
0
 
LVL 13

Expert Comment

by:rhickmott
ID: 17965610
If you want to use \ in a regular expression without it taking it literally or escaping the next character you need to escape the \ with a \ so \\ :)
0
 

Author Comment

by:MaxDisk
ID: 17970413
I tried the \\ already but  found an error.

Warning: preg_match() [function.preg-match]: Compilation failed: missing terminating ] for character class at offset 25 in

<?
if (!isset($_SESSION)) session_start();
$_SESSION['Test']="sds \'";
if(!preg_match("/^[a-zA-Z0-9 \-\ \\]{1,45}$/", stripslashes($_SESSION['Test'])) == 1){
echo "error<br>";
echo $_SESSION['Test'];
}
?>
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 300 total points
ID: 17970714
can you tell us what you meant to "match" with the last part of your expression:
" \-\ "

what about this:

<?
if (!isset($_SESSION)) session_start();
$_SESSION['Test']="sds \'";
if(!preg_match("/^[a-zA-Z0-9 \\\-]{1,45}$/", stripslashes($_SESSION['Test'])) == 1){
echo "error<br>";
echo $_SESSION['Test'];
}
?>
0
 

Author Comment

by:MaxDisk
ID: 17976451

Sorry abou the confusing message.
What I really want is allow user to enter the backlash in the input field, but the problem is I don't know how to allow the preg_match to EXCLUDE the \.

But you solved my problem already

//Final piece
<?
if (!isset($_SESSION)) session_start();
$_SESSION['Test']="sds \ ";

if(!preg_match("/^[ \\ a-zA-Z0-9]{1,45}$/", stripslashes($_SESSION['Test'])) == 1){
echo "error<br>";
echo $_SESSION['Test'];
}
else
{
echo "Input accepted <br>";
echo $_SESSION['Test'];
}

?>
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now