Solved

Regex Replacement

Posted on 2016-07-19
9
71 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to dynamically set the form action using jQuery.
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 …

627 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