Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to accept the \ "backslash" with the preg_match

Posted on 2006-11-16
5
Medium Priority
?
1,194 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 900 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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

722 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