Solved

Need a PHP regular expression that matches variables outside tags

Posted on 2011-03-21
11
316 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
[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
  • 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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

Technology Partners: 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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
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.
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…
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…

728 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