Jump to content

preg_replace


kat32

Recommended Posts

Vague examples again

yer code shows yer preg patterns

yet no example of wut the variables are, $msg

I expanded yer code to this

<?php
header('Content-type: text/plain');
//$alpha = 'apple';
//$beta = 'bob';
echo $msg = "{alpha} {beta} clone\n";
echo $msg2 = preg_replace("/\{(\w+)\}/e", "\$\\1", $msg);
?>

When I tried it I got this

/**

{alpha} {beta} clone

Notice: Undefined variable: alpha in G:\PHP\QuickPHP\htdocs\time.php(4) : regexp code on line 1

 

Call Stack:

    0.2825      57800  1. {main}() G:\PHP\QuickPHP\htdocs\time.php:0

    0.2826      58144  2. preg_replace() G:\PHP\QuickPHP\htdocs\time.php:4

    0.2828      59472  3. preg_replace() G:\PHP\QuickPHP\htdocs\time.php:4

 

 

Notice: Undefined variable: beta in G:\PHP\QuickPHP\htdocs\time.php(4) : regexp code on line 1

 

Call Stack:

    0.2825      57800  1. {main}() G:\PHP\QuickPHP\htdocs\time.php:0

    0.2826      58144  2. preg_replace() G:\PHP\QuickPHP\htdocs\time.php:4

    0.5247      59696  3. preg_replace() G:\PHP\QuickPHP\htdocs\time.php:4

 

  clone

 

PHP Notice:  Undefined variable: alpha in G:\PHP\QuickPHP\htdocs\time.php(4) : regexp code on line 1

PHP Stack trace:

PHP  1. {main}() G:\PHP\QuickPHP\htdocs\time.php:0

PHP  2. preg_replace() G:\PHP\QuickPHP\htdocs\time.php:4

PHP  3. preg_replace() G:\PHP\QuickPHP\htdocs\time.php:4

PHP Notice:  Undefined variable: beta in G:\PHP\QuickPHP\htdocs\time.php(4) : regexp code on line 1

PHP Stack trace:

PHP  1. {main}() G:\PHP\QuickPHP\htdocs\time.php:0

PHP  2. preg_replace() G:\PHP\QuickPHP\htdocs\time.php:4

PHP  3. preg_replace() G:\PHP\QuickPHP\htdocs\time.php:4

 

*/

 

So I added $alpha and $beta variables (uncomment from code sample)

and then i got

 

{alpha} {beta} clone
apple bob clone

 

so I dunno the problem

Link to comment
Share on other sites

<?php
header('Content-type: text/plain');
//$alpha = 'apple';
//$beta = 'bob';
echo $msg = "{alpha} {beta} clone\n";
echo $msg2 = preg_replace("/\{(\w+)\}/e", "\$\\1", $msg);
?>

 

You don't need to escape the { and } characters (this is a common misconception.. under rare circumstances you would, but not in this case). Nor does the dollar sign need escaping...

 

$alpha = 'apple';
$beta = 'bob';
echo $msg = "{alpha} {beta} clone\n";
echo $msg2 = preg_replace("/{(\w+)}/e", "$\\1", $msg);

 

Output:

{alpha} {beta} clone apple bob clone

 

Same difference though..

 

EDIT - Note to OP, please provide more information (as laffin mentions.. your example is vague).

 

 

Link to comment
Share on other sites

  • 8 months later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.