Link to home
Start Free TrialLog in
Avatar of Arakelian Jerome
Arakelian Jerome

asked on

replace coding with preg_replace

Hello,

I want to replace that which is between

function udm_(a){ (.*) } with preg_replace ().

Here is my code that does not work my.

$body = preg_replace ("/function udm_(a){ (.*)} / i", 'function udm_(a){ }', $body);
SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You probably need preg_quote().  I'll try to give you a tested-and-working code sample in a moment.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can do this in code as shown below. Full sample here
<?php
$body = <<< TEXT
function someotherfunction() {
}

function udm_(a) {
  var somevalue;
  somevalue = somevalue * 3;
  if (somevalue > 10) {
    somevalue = 10;
  }
  alert(somevalue);
}
function someotherfunction() {
  var thismuststay = 10;
}
TEXT;

$searchstring = "function udm_(a) {";
$start = strpos($body, $searchstring) + strlen($searchstring);
$stack = 1;
$len = strlen($body);
for($index = $start;$index < $len; $index++) {
  if ($body[$index] == '{') {
    $stack++;
  }
  else if ($body[$index] == '}') {
    $stack--;
  }
  if ($stack == 0) break;
}

$output = substr($body, 0, $start) . substr($body, $index);
?>

Open in new window

Avatar of Arakelian Jerome
Arakelian Jerome

ASKER

Thank you , I just succeed with your code.