Solved

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

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

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 110

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 110

Expert Comment

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

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

733 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