Solved

Regex Replacement

Posted on 2016-07-19
9
68 Views
Last Modified: 2016-07-20
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
0
Comment
Question by:Stephen Forlance
[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
  • 3
  • 2
  • +1
9 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41719039
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

0
 

Author Comment

by:Stephen Forlance
ID: 41719158
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
0
 
LVL 35

Accepted Solution

by:
Dan Craciun earned 400 total points
ID: 41719191
$result = preg_replace('/(?=\[PHP\])(.*?)&quot;(.*?)&quot;(.*)/m', '$1\'$2\'$3', $subject);

HTH,
Dan
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 41719199
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.
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 100 total points
ID: 41719237
Or just modify Dan's example to inject double quotes instead of apostrophes.  But as you think about this problem, be aware of this humorous but very, very wise advice.  If there is another starting point that does not require string parsing, you might want to address the problem from that point instead!
<?php // demo/temp_forlance.php
/**
 * https://www.experts-exchange.com/questions/28958241/Regex-Replacement.html
 * https://www.experts-exchange.com/questions/28958241/Regex-Replacement.html#a41719191
 *
 * https://www.explainxkcd.com/wiki/index.php/1171:_Perl_Problems
 */
error_reporting(E_ALL);

// 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;


// MAKE THE TRANSLATION ACCORDING TO #a41719191
$rgx = '/(?=\[PHP\])(.*?)&quot;(.*?)&quot;(.*)/m';
$rep = '$1"$2"$3';
$outgo = preg_replace($rgx, $rep, $input);


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

echo PHP_EOL . htmlentities($outgo);
echo PHP_EOL;

Open in new window

0
 
LVL 14

Expert Comment

by:frankhelk
ID: 41719684
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.
0
 

Author Comment

by:Stephen Forlance
ID: 41719958
Frank thanks for the recommendation, I used to use Regex Buddy, but since moving to a Mac havent managed to find anything as good
0
 
LVL 35

Expert Comment

by:Dan Craciun
ID: 41720232
Why don't you simply run a Windows VM and use your favorite Windows software?
1
 

Author Comment

by:Stephen Forlance
ID: 41720944
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    :-)
0

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 dynamically set the form action using jQuery.

739 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