Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

preg match php problem!

Posted on 2010-09-09
3
Medium Priority
?
405 Views
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?";
  print_r($matches);
    $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

0
Comment
Question by:devteam
[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 Comments
 
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
0
 
LVL 35

Accepted Solution

by:
Terry Woods earned 2000 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?";
  print_r($matches);
    $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

0
 
LVL 1

Author Closing Comment

by:devteam
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 :)
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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.
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…
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 …

688 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