Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Regex Replacement

Posted on 2016-07-19
9
Medium Priority
?
78 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 111

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 1600 total points
ID: 41719191
$result = preg_replace('/(?=\[PHP\])(.*?)&quot;(.*?)&quot;(.*)/m', '$1\'$2\'$3', $subject);

HTH,
Dan
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 111

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 111

Assisted Solution

by:Ray Paseur
Ray Paseur earned 400 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

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!

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

824 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