Solved

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

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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.
The viewer will learn how to count occurrences of each item in an array.
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.

690 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