PHP preg_replace()

  • Azoundria
  • Professor
  • Professor
  • User avatar
  • Posts: 907
  • Loc: Calgary, Alberta, Canada

Post 3+ Months Ago

Here's what I entered:

Code: [ Select ]
$contents = preg_replace("\{[-0-9a-zA-Z]+\}", "<?php include \"$1.php\"; ?\>", $contents);


I want to make the content change this: {absolution} to this:

Code: [ Select ]
<?php include "absolution.php"; ?>


And this: {myFile-124} to this:

Code: [ Select ]
<?php include "myFile-124.php"; ?>


It's a shortcut for including files much faster.

It's not working, and I've been stuck on it all morning. Any help is great.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9086
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

You need to put () around the parts that you want to use for replacement. So with the above I believe this will make it work:

Code: [ Select ]
$contents = preg_replace("\{([-0-9a-zA-Z]+)\}", "<?php include \"$1.php\"; ?\>", $contents);

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: ScottG and 89 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.