preg match php problem!

Posted on 2010-09-09
Last Modified: 2012-05-10
We're trying to do a basic preg match in a file, if the preg match is false, we want to add a line in a file, and then if we re-run the preg match and the line already exists, then just skip that part...

for some reason it ALWAYS goes in the IF statement.... Some help would be appreciated!

we tried preg_match_all, preg_match, strpos, strstr..

string to look for:
exten => h,n,AGI(e911/hangup_check.agi,${UNIQUEID},${LEG_ID},${EPOCH})

string to look for to replace:
exten => h,n,AGI(e911/911cdr.agi,${UNIQUEID})

string to replace if above line is found:
exten => h,n,AGI(e911/hangup_check.agi,${UNIQUEID},${LEG_ID},${EPOCH})
exten => h,n,AGI(e911/911cdr.agi,${UNIQUEID})
$inputSED3 = file_get_contents("/opt/911Gateway/sbc_etc/extensions_911.conf");

if ( preg_match_all("/exten => h,n,AGI(e911\/hangup_check.agi,\${UNIQUEID},\${LEG_ID},\${EPOCH})/", $inputSED3, $matches) === 0 )


  echo "in?";


    $input = preg_replace('/exten => h,n,AGI\(e911\/911cdr\.agi,\${UNIQUEID}\)/', "exten => h,n,AGI(e911/hangup_check.agi,\${UNIQUEID},\${LEG_ID},\${EPOCH})\nexten => h,n,AGI(e911/911cdr.agi,\${UNIQUEID})", $inputSED3);

    file_put_contents("/opt/911Gateway/sbc_etc/extensions_911.conf", $input);


Open in new window

Question by:devteam
LVL 31

Expert Comment

by:Marco Gasi
ID: 33642303
Try this reg exp

"//(exten => h,n,AGI\(e911\/hangup_check.agi,\$\{UNIQUEID\},\$\{LEG_ID\},\$\{EPOCH\}\))/igm/"

Best regards
LVL 35

Accepted Solution

Terry Woods earned 500 total points
ID: 33642308
There are special characters in the string you're searching for that affect the pattern, and you haven't escaped them all. You can use preg_quote to do the hard work for you. Try this:

$inputSED3 = file_get_contents("/opt/911Gateway/sbc_etc/extensions_911.conf");

$find = 'exten => h,n,AGI(e911/hangup_check.agi,${UNIQUEID},${LEG_ID},${EPOCH})';

if ( preg_match_all("/".preg_quote($find, "/")."/", $inputSED3, $matches) === 0 )


  echo "in?";


    $input = preg_replace('/'.preg_quote('exten => h,n,AGI(e911/911cdr.agi,${UNIQUEID})','/').'/', "exten => h,n,AGI(e911/hangup_check.agi,\${UNIQUEID},\${LEG_ID},\${EPOCH})\nexten => h,n,AGI(e911/911cdr.agi,\${UNIQUEID})", $inputSED3);

    file_put_contents("/opt/911Gateway/sbc_etc/extensions_911.conf", $input);


Open in new window


Author Closing Comment

ID: 33646796
Thank you for the quick response, that works like a charm.. I wasn't aware of this preg_quote!! that's very helpful :)

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
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.

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

23 Experts available now in Live!

Get 1:1 Help Now