Solved

How to accept the \ "backslash" with the preg_match

Posted on 2006-11-16
5
1,187 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

WordPress Tutorial 1: Installation & Setup

WordPress is a very popular option for running your web site and can be used to get your content online quickly for the world to see. This guide will walk you through installing the WordPress server software and the initial setup process.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

617 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