Solved

Need a PHP regular expression that matches variables outside tags

Posted on 2011-03-21
11
313 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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

808 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