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

x
?
Solved

Need a PHP regular expression that matches variables outside tags

Posted on 2011-03-21
11
Medium Priority
?
320 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
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!

 
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
 
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 2000 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

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…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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.
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 …
Suggested Courses

885 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