[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2013-12-14
5
Medium Priority
?
381 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
[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
  • 3
  • 2
5 Comments
 
LVL 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

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 111

Expert Comment

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

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

650 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