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);
Arakelian JeromeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
It is not that simple - consider the following input
$body = <<< TEXT
function udm_(a) {
  var somevalue;
  somevalue = somevalue * 3;
  alert(somevalue);
}
TEXT;

Open in new window

Then this would work
$body = preg_replace ("/function udm_\(a\) \{[\s\S]*?\}/ i", "function udm_(a){ }", $body);

Open in new window

But what if your input looked like this
$body = <<< TEXT
function udm_(a) {
  var somevalue;
  somevalue = somevalue * 3;
  if (somevalue > 10) {
    somevalue = 10;
  }
  alert(somevalue);
}
function someotherfunction() {
  var thismuststay = 10;
}
TEXT;

Open in new window

The nested { } will throw a spanner in the works.
You might need to look at what else there is in the text to identify the termination point for the greedy search.
1
Ray PaseurCommented:
You probably need preg_quote().  I'll try to give you a tested-and-working code sample in a moment.
0
Ray PaseurCommented:
This works, given what we have to work with, but as Julian points out, the devil is in the details.  And we do not have much in the way of details.  My recommendation is to step back from this micro-problem and just tell us in plain language what your problem is here.  I am almost 100% certain that there is a better way than using regular expressions modifying computer code!
http://iconoun.com/demo/temp_arakelian.php
<?php // demo/temp_arakelian.php

/**
 * http://www.experts-exchange.com/questions/28710568/replace-coding-with-preg-replace.html
 */
error_reporting(E_ALL);


// FAILS: $body = preg_replace ("/function udm_(a){ (.*)} / i", 'function udm_(a){ }', $body);

// TEST DATA
$dat = <<<EOD
function udm_(a){
    // SOME INFORMATION HERE
    // MAYBE JAVASCRIPT?
}
EOD;

// A REGULAR EXPRESSION TO NULLIFY THE FUNCTION DEFINITION
$rgx
= '#'               // REGEX DELIMITER
. 'function udm_'   // STRING LITERAL
. preg_quote('(a)') // ESCAPED STRING
. preg_quote('{')   // ESCAPED STRING
. '(.*?)'           // ANYTHING OR NOTHING
. preg_quote('}')   // ESCAPED STRING
. '#'               // REGEX DELIMITER
. 'is'              // FLAGS - CASE-INSENSITIVE, SINGLE LINE
;

// A REPLACEMENT NULLIFIED DEFINITION
$rep = 'function udm_(a){ }';

// PERFORM THE REPLACEMENT
$new = preg_replace($rgx, $rep, $dat);

// SHOW THE WORK PRODUCT
echo '<pre>';
echo PHP_EOL . 'OLD: ' . htmlentities($dat);
echo PHP_EOL . 'NEW: ' . htmlentities($new);
echo PHP_EOL;
echo PHP_EOL . 'RGX: ' . htmlentities($rgx);

Open in new window

Regular expressions are not sufficiently powerful to be used on computer code, or self-aware documents like (X)HTML.  This note, while a little tongue-in-cheek explains it about as well as it can be explained.  What you almost certainly need is a state engine, not a regular expression.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Julian HansenCommented:
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

0
Arakelian JeromeAuthor Commented:
Thank you , I just succeed with your code.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.