Solved

Need a PHP regular expression that matches variables outside tags

Posted on 2011-03-21
11
311 Views
Last Modified: 2012-05-11
I have the following text

<p>Sample text that contains a {myvariable} string and {variable} string</p>
<textarea cols='40' rows='4'>This text comes from an outside file but uses the same {myvariable} string.</textarea>
<p>Additional text that contains different {variable} strings</p>

I'm using the following code to replace the strings with variables defined elsewhere:

foreach($arrTags as $tag){
$myvar = preg_replace('/(?!\{'.$tag.'\}[^<textarea]*<\/textarea>)\{'.$tag.'\}/',$$tag,$myvar);
}

I can't seem to get it to NOT replace the {myvariable} tags that fall between the <textarea> tags. What am I missing?
0
Comment
Question by:tmcclint
  • 5
  • 3
  • 3
11 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 35185158
Try this:
foreach($arrTags as $tag){
    $myvar = preg_replace('/(?!\{'.$tag.'\}[^<]*<\/textarea>)\{'.$tag.'\}/',$$tag,$myvar);
}

Open in new window

0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 35185172
[^<textarea] means one character that is not any of these: <texra

In:
<textarea cols='40' rows='4'>This text comes from an outside file but uses the same {myvariable} string.</textarea>
There is a t in " string." (after {myvariable}, but before the </textarea> tag) so the negative lookahead fails to match.
0
 

Author Comment

by:tmcclint
ID: 35185457
My text includes the textarea tags but what falls in between can span an indeterminate number of lines. While the solution you sent works on a single line, is it possible to have it look ahead or behind over several lines? So see this text:

<html>
<body>
<p>my sample {myvariable} text</p>
<textarea cols='40' rows='4'>
<html>
<body>
<p>This is 1 line</p>
<p>This is another with the {myvariable} string in it.
<p>More Text</p>
</body>
</html>
</textarea>
<p>More text with the {variable} string in it.</p>

What I'm looking for would be this:

<html>
<body>
<p>my sample REPLACED text</p>
<textarea cols='40' rows='4'>
<html>
<body>
<p>This is 1 line</p>
<p>This is another with the {myvariable} string in it.
<p>More Text</p>
</body>
</html>
</textarea>
<p>More text with the REPLACED string in it.</p>

Right now it still seems to be catching the {myvariable} string at line 8.
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 35185489
Adding the s pattern modifier *might* work - I will test it in 30mins or so if I don't hear back from you:
    $myvar = preg_replace('/(?!\{'.$tag.'\}[^<]*<\/textarea>)\{'.$tag.'\}/s',$$tag,$myvar);
0
 

Author Comment

by:tmcclint
ID: 35186224
Nope, the /s didn't do it. It's still catching the internal {myvariable}.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35186597
Is there a reason why you can't just do a simple string replace using str_replace?
0
 

Author Comment

by:tmcclint
ID: 35189749
How would str_replace look to see if what it was replacing was within the <textarea> tags?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 35190195

>>  How would str_replace look to see if what it was replacing was within the <textarea> tags?

Ah. I didn't realize you wanted to conditionally replace based on where a particular variable occurs. Try this out:
$src = preg_replace('/\{'.$tag.'\}(?!(?:.(?!<textarea))+?<\/textarea>)/s', $$tag, $myvar);

Open in new window

0
 

Author Comment

by:tmcclint
ID: 35190371
It still doesn't seem to catch what I'm look for. I tried this:

preg_replace('/(?!<textarea)\{'.$tag.'\}*(?!<\/textarea>)/s',$$tag,$myvar);

But it only seems to catch {myvariable} strings that are NOT preceded by <textarea> and not ones that occur after the </textarea>.

In this string:
<p>This is a real {string}</p>
<textarea id='taEditor' name='fileEditor' cols='80' rows='30'><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>{sitename} : {title}</title>
</textarea>
<p>{test}</p>

Open in new window


It only catches {string} and NOT {test}. At least it appears to be skipping the ones that fall within the TEXTAREA tags.
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 35190757
>>  preg_replace('/(?!<textarea)\{'.$tag.'\}*(?!<\/textarea>)/s',$$tag,$myvar);

That's not going to work the way you think it will anyway.


I scaled down the test since I don't have your exact setup, but I don't perceive a reason why it shouldn't work. This is the test I performed (TextArea expanded to see all text):
<html>
	<body>
	<?php

		$myvar = "<html>
		<body>
		<p>my sample {myvariable} text</p>
		<textarea cols='40' rows='15'>
		<html>
		<body>
		<p>This is 1 line</p>
		<p>This is another with the {myvariable} string in it.
		<p>More Text</p>
		</body>
		</html>
		</textarea>
		<p>More text with the {myvariable} string in it.</p>";

		$myvar = preg_replace('/\{myvariable\}(?!(?:.(?!<textarea))+?<\/textarea>)/s', 'AWESOME', $myvar);

		echo $myvar;
	?>
	</body>
</html>

Open in new window

untitled.PNG
0
 

Author Closing Comment

by:tmcclint
ID: 35191936
That was what I needed. I was able to utilize the code he presented and come to the solution. Thanks a million.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

930 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

22 Experts available now in Live!

Get 1:1 Help Now