Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 411
  • Last Modified:

preg match php problem!

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
devteam
Asked:
devteam
1 Solution
 
Marco GasiFreelancerCommented:
Try this reg exp

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

Best regards
0
 
Terry WoodsIT GuruCommented:
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
 
devteamAuthor Commented:
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now