Solved

How to accept the \ "backslash" with the preg_match

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 count occurrences of each item in an array.

810 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