Solved

Need a PHP regular expression that matches variables outside tags

Posted on 2011-03-21
11
309 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 74

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 74

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 74

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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 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 …

760 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

17 Experts available now in Live!

Get 1:1 Help Now