Fog Juice Posted November 20, 2007 Share Posted November 20, 2007 $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. Quote Link to comment https://forums.phpfreaks.com/topic/78148-solved-convert-magic-quote-string-to-non-magic-quote-string/ Share on other sites More sharing options...
revraz Posted November 20, 2007 Share Posted November 20, 2007 What happens when you really want to use ' ' ? Quote Link to comment https://forums.phpfreaks.com/topic/78148-solved-convert-magic-quote-string-to-non-magic-quote-string/#findComment-395486 Share on other sites More sharing options...
Fog Juice Posted November 20, 2007 Author Share Posted November 20, 2007 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". Quote Link to comment https://forums.phpfreaks.com/topic/78148-solved-convert-magic-quote-string-to-non-magic-quote-string/#findComment-395489 Share on other sites More sharing options...
MadTechie Posted November 20, 2007 Share Posted November 20, 2007 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/78148-solved-convert-magic-quote-string-to-non-magic-quote-string/#findComment-395500 Share on other sites More sharing options...
Fog Juice Posted November 20, 2007 Author Share Posted November 20, 2007 awesome. Ur great. Solved! Quote Link to comment https://forums.phpfreaks.com/topic/78148-solved-convert-magic-quote-string-to-non-magic-quote-string/#findComment-395504 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.