Solved

How to escape # using preg_match. Simple backslash doesn't seem to work?

Posted on 2013-12-14
5
364 Views
Last Modified: 2013-12-14
Hi,
I'm trying to test if a string matches the word #yesterday (including the pound sign). I am finding if I don't have the pound sign in and the $body equals 'yesterday' works fine. But with the # sign, doesn't.

if( preg_match('/^(#yesterday)$/i', $body) ){

How (I'm stuck in 5.2+ php) can I see if the string is yesterday with the pound sign?

Thanks,
0
Comment
Question by:tjyoung
  • 3
  • 2
5 Comments
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 39718802
/#yesterday/i says match any string containing the literal string #yesterday in case-insensitive mode.

See http://www.laprbass.com/RAY_temp_tjyoung.php

If you want to post your test data set, I'll give you a script that will do the job.

<?php // RAY_temp_tjyoung.php
error_reporting(E_ALL);

// SEE http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28318272.html
// SEE http://www.php.net/manual/en/function.preg-match.php
// SEE http://www.php.net/manual/en/reference.pcre.pattern.syntax.php

$testdata = array
( 'yesterday'
, 'The Beatles #Yesterday'
, 'Embedded #yesterday in a string'
, '#YesTerDay'
, 'Gooseball'
)
;

$rgx
= '/'           // REGEX DELIMITER
. '#yesterday'  // LITERAL STRING
. '/'           // REGEX DELIMITER
. 'i'           // CASE-INSENSITIVE MATCH
;


foreach ($testdata as $str)
{
    echo PHP_EOL . "<br>$str ";
    if (preg_match($rgx, $str)) echo '<-- MATCH HERE';
}

Open in new window

0
 
LVL 1

Author Comment

by:tjyoung
ID: 39718815
I'm using Twilio and when an incoming text contains certain keywords ie: #today, #yesterday, #week or #month, I'm responding back with how many car deals were done in that period of time. So the user texts just the one word with the pound sign in front so my app knows its an instruction and not just a 'word' like yesterday.

I test the $to number to be sure its an admin phone number, than check the
$body = $_REQUEST['Body']; against those keywords.

Kind of like:
if( preg_match('/^(today)$/i', $body) ){
send back deals today
} else if if( preg_match('/^(yesterday)$/i', $body) ){
send back yesterdays deal count
} else {
do whatever
}

Works fine until I add in the # pound sign.

I tried placing the slash but didn't seem to work:
if( preg_match('/^(/#yesterday$/i', $body) ){

just didn't return anything.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39718817
I understand the issue; I just need to see the actual test data.  You can get this with var_dump().  Then use view source and copy/paste the information here.  When your regex has the ^ and $ at the beginning and end, you're telling the engine to evaluate the entire string.  If there are any other characters, even invisible whitespace, the match will fail.

Philosophy on why the test data matters (much more than the failing code):
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_7830-A-Quick-Tour-of-Test-Driven-Development.html
0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 39718836
That worked perfectly as the how to example. Thanks again.
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39718839
Great!  Thanks for the points and thanks for using EE, ~Ray
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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…
The viewer will learn how to dynamically set the form action using jQuery.

911 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

18 Experts available now in Live!

Get 1:1 Help Now