php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #12477 preg_replace evaluates dollar signs as variables (eg $1,000 becomes ,000)
Submitted: 2001-07-31 00:40 UTC Modified: 2001-08-06 18:53 UTC
From: Grant dot Walters at walters dot co dot nz Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 4.0.6 OS: Linux and SCO
Private report: No CVE-ID: None
 [2001-07-31 00:40 UTC] Grant dot Walters at walters dot co dot nz
The setup for my templating system:

$template=array(
  "/({PAGETOP})/",
  "/({PAGENAVBAR})/",
  "/({PAGEMAIN})/",
  "/({PAGELINKS})/",
  "/({PAGESPACE})/",
  "/({PAGETEXT})/",
  "/({PAGENEWS})/",
  "/({PAGEBOTTOM})/",
  "/({PAGEDATA})/"
  );

$pagevars=array(
  "PAGETOP" => get_page($site->page_top),
  "PAGENAVBAR" => get_page($site->page_top),
  "PAGEMAIN" => get_page($site->page_main),
  "PAGELINKS" => get_page($site->page_links),,
  "PAGESPACE" => get_page($site->page_space),
  "PAGETEXT" => get_page($site->page_text),
  "PAGENEWS" => get_page($site->page_news),,
  "PAGEBOTTOM" => get_page($site->page_bottom),
  "PAGEDATA" => ""
  );

$page = "{PAGETOP}{PAGENAVBAR}{PAGEMAIN}{PAGEBOTTOM}";

$page = preg_replace($template,$pagevars,$page);

echo $page;

Basically each physical page referenced via the get_page function is a text template that may contain HTML, text or variables in the form {VAR}.

Eventually content from a database ends up in a variable called PAGEDATA which is in the template stored in the page main variable.

The problem:

PAGEDATA ends up containing page text and it has something like "would cost around $16,000 to complete" in that text.

When the preg_replace is performed, something somewhere is assuming that $16 is a variable (which doesn't exist) and the text ends up as "would cost around ,000 to complete"



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2001-08-06 12:49 UTC] andrei@php.net
That's right, you need to escape $ in your replacement strings.
 [2001-08-06 17:24 UTC] Grant dot Walters at walters dot co dot nz
Is it possible to ask that the this be included as a new feature?

A switch to tell the PCRE engine? to ignore variable substitution?

It would make things a lot easier, I think.

Thanks
 [2001-08-06 17:26 UTC] andrei@php.net
If you don't want to use \n or $n substitution, then maybe you are better off using str_replace()?
 [2001-08-06 17:26 UTC] andy@php.net
reopened and reclassified as a feature request.
 [2001-08-06 17:29 UTC] andrei@php.net
To QA people (Andy): please don't be hasty.
 [2001-08-06 18:53 UTC] Grant dot Walters at walters dot co dot nz
I was trying to have to walk the array twice.

The following works:

$pagevars = str_replace("$","\\\$",$pagevars);
$page = preg_replace($template,$pagevars,$page);

The reason for using preg_replace is that it does a recursive? replace.

My pagevars array actually has about 40 variables and the intention is to allow website designers to define their own additional variables that are added to the template string.

Thanks for your help.



 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Mar 29 05:01:28 2024 UTC