Jump to content

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


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;
?>

 

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.