Solved

Regex Replacement

Posted on 2016-07-19
9
54 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
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 108

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 34

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
 
LVL 108

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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 108

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 13

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 34

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

759 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now