Solved

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

Posted on 2013-12-14
5
361 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
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…
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.

759 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

23 Experts available now in Live!

Get 1:1 Help Now