Jump to content

[SOLVED] convert magic quote string to non-magic quote string...


Fog Juice

Recommended Posts

$firstname = "Chris";

 

Is there a way to convert a string that looks like $string = 'Hello my name is $firstname';

 

to something like this $string = "Hello my name is $firstname";

 

So that way, with the "" it will be able to display $firstname as Chris (see first line)

 

 

or does anyone have some prewritten code that scans through a piece of text and replaces all of the $firstname with it's text value, which in this case, is "Chris". It would save me a lot of time from writing one if someone would be so kind as to provide one, or have something similar that I could modify.

What happens when you really want to use ' ' ?

$firstname = "Chris";

 

When you use ' ', it displays $firstname as $firstname, but when you use " " it displays $firstname as Chris. I'm looking for something that will convert text from single quotes to double quotes so that when I pull something from a database that says 'Hello, welcome $firstname', I can print it out as "Hello, welcome Chris".

 

 

you could do this

<?php
$firstname = "Chris";
$string = 'Hello my name is $firstname';
preg_match_all('/\$(\w+)/si', $string, $result, PREG_PATTERN_ORDER);
for ($i = 0; $i < count($result[0]); $i++)
{
$rep = $result[1][$i];
$string = str_replace("$$rep", $$rep, $string);
}

echo $string;
?>

 

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.