Link to home
Start Free TrialLog in
Avatar of Stephen Forlance
Stephen Forlance

asked on

Regex Replacement

Hi all,

I have a bit of text that I want to run a regex place on, but I cant get my head around this.

<html>
<title>Blah</title>
<body>
<h1>Hello</h1>

[PHP] include(&quot;file.php&quot;); [-PHP]

[PHP] echo $date;[-PHP]

[PHP] include(&quot;something.php&quot;); [-PHP]

</body>
</html>

Open in new window


What I would like to do is run a regex that replaced the &quot; with "  if they are between [PHP] and [-PHP], for everytime it occurs, and then return the new string as $content
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Recommend you take the easy way out and get results instead of confusion!  Check the comments for an explanation of why experienced programmers try to avoid complicated problems like string processing and avoid complicated solutions like regular expressions.
https://iconoun.com/demo/temp_forlance.php
<?php // demo/temp_forlance.php
/**
 * https://www.experts-exchange.com/questions/28958241/Regex-Replacement.html
 *
 * https://www.explainxkcd.com/wiki/index.php/1171:_Perl_Problems
 */
error_reporting(E_ALL);
echo '<pre>';

// THE TEST DATA
$date = date('c');
$input = <<<EOD
<html>
<title>Blah</title>
<body>
<h1>Hello</h1>

[PHP] include(&quot;file.php&quot;); [-PHP]

[PHP] echo $date;[-PHP]

[PHP] include(&quot;something.php&quot;); [-PHP]

</body>
</html>
EOD;


// THE EASY SOLUTION
$alpha = '(&quot;';
$omega = '&quot;)';
$outgo = $input;
$outgo = str_replace($alpha, '("', $outgo);
$outgo = str_replace($omega, '")', $outgo);

// SHOW THE WORK PRODUCTS
echo htmlentities($input);
echo '<br>' . PHP_EOL;

echo htmlentities($outgo);

Open in new window

Outputs:
<html>
<title>Blah</title>
<body>
<h1>Hello</h1>

[PHP] include(&quot;file.php&quot;); [-PHP]

[PHP] echo 2016-07-19T06:15:14-05:00;[-PHP]

[PHP] include(&quot;something.php&quot;); [-PHP]

</body>
</html>

<html>
<title>Blah</title>
<body>
<h1>Hello</h1>

[PHP] include("file.php"); [-PHP]

[PHP] echo 2016-07-19T06:15:14-05:00;[-PHP]

[PHP] include("something.php"); [-PHP]

</body>
</html>

Open in new window

Avatar of Stephen Forlance
Stephen Forlance

ASKER

Hi Ray,
Thanks for this, I should have been clearer in my code, but there would be cases where &quot; would appear normally, so I only wanted it replaced if it was between [PHP] and [-PHP], that why I tried dabbling with regex
ASKER CERTIFIED SOLUTION
Avatar of Dan Craciun
Dan Craciun
Flag of Romania 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
Regex is one way to deal with this, another (and to my mind simpler) is a state engine.  However all of my state engine examples use characters instead of substrings.  Give me a few moments and I'll come up with a good example.
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
Just a side comment ... if you need to build a regular expression, I recommend the magnificent tool Expresso (see here).

With some basic knowledge of the regex concept it allows to design regular expression for searching and replacement and allows testing on custom text samples.

It can even genrate code for different programming languages and contais a set of example in a regex library.
Frank thanks for the recommendation, I used to use Regex Buddy, but since moving to a Mac havent managed to find anything as good
Why don't you simply run a Windows VM and use your favorite Windows software?
Dan, because I view my  relationship with Microsoft as over, and almost treat them like a pyscho ex, that I am happy to see the back of    :-)