Solved

How to accept the \ "backslash" with the preg_match

Posted on 2006-11-16
5
1,179 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 143

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 143

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

809 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