interesting preg_replace problem
| From: | Rex A. Roof | Date: | Wed, 11 Oct 2000 04:38:30 +0000 |
| Subject: | interesting preg_replace problem | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-19491@lists.php.net to get a copy of this message | ||
right off the bat, here's my preg_replace call:
preg_replace('/\{([^}]+)}/e',
"'{<a href=view_entry.php?word='. urlencode(trim('\\1'))
.'>\\1</a>}'",
$string);
what I'm doing is toying with the jargon file (www.jargon.org), just
dumping into a mysql database and pulling it out and parsing it for
the web. Nothing new, just playing with it for the hell of it.
my problem is that I have data that looks like this in $string:
1. vi. [orig. from BASIC's
CHAIN' statement] To hand off execution to a child or
successor without going through the {OS} command interpreter that invoked it. The state of the
parent program is lost and there is no returning to it. Though this facility used to be common on
memory-limited micros and is still widely supported for backward compatibility, the jargon usage is
semi-obsolescent; in particular, most Unix programmers will think of this as an {exec}. Oppose the
more modern subshell'. 2. n. A series of linked data areas within an operating system
or application. `Chain rattling' is the process of repeatedly running through the linked data
areas searching for one which is of interest to the executing program. The implication is that there
is a very large number of links on the chain.
and, this preg_replace call is turning it into this:
1. vi. [orig. from BASIC's CHAIN' statement] To hand off execution to a child or
successor without going through the {<a href=view_entry.php?word=OS>OS</a>} command
interpreter that invoked it. The state of the parent program is lost and there is no returning to
it. Though this facility used to be common on memory-limited micros and is still widely supported
for backward compatibility, the jargon usage is semi-obsolescent; in particular, most Unix
programmers will think of this as an {<a href=view_entry.php?word=%60Ch> Ch</a>}.
Oppose the more modern subshell'. 2. n. A series of linked data areas within an
operating system or application. Chain rattling' is the process of repeatedly running
through the linked data areas searching for one which is of interest to the executing program. The
implication is that there is a very large number of links on the chain.
the problem here is that it's replacing the {exec} with a hyperlinked {`Ch},
which is from later in the string. This is what it does with every one of
these. the first {} pattern matches and replaces fine, but any {} pattern
after that gets corrupted with data from later in the string. If its at
the end of the string, it gets replaces with a long string of Zs, which is
very odd. I'm trying to just link like the first {OS} gets linked.
I'm sure it's just something silly in my regex, anyone spot it? I'm stumped.
-Rex