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
https://forums.phpfreaks.com/topic/154299-preg_replace/#findComment-811243
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
https://forums.phpfreaks.com/topic/154299-preg_replace/#findComment-811259
Share on other sites

  • 8 months later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.