MrFish Posted September 21, 2011 Share Posted September 21, 2011 I'm trying to use php eval on this string but I can't get it to work. I'm trying to locate anything within (( ... )) in a string and replace it with (($data[' ... '])) but running eval on that only returns the same text. Here is my test code: $string = preg_replace_callback("/\(\(([^)]+)\)\)/", 'insertDummy', $string); echo $string; //TEST WORKS $test = "(({\$data['address']}))"; echo $test; eval("\$test = \"$test\";"); echo $test; //BACK TO CODE eval("\$string = \"$string\";"); echo $string; Here is the string before I run preg_replace_callback <div class="horizontal_results" id="horizontal_results"> <div class="horizontal_results_top"></div> <div class="horizontal_results_content"> <div class="horizontal_results_col01"> <img src="((photo))" alt="" width="150" height="113" class="left" style="margin-left: 5px;"> </div> <div class="horizontal_results_right"> <h4>((address))</h4> <div class="horizontal_results_col02"> <h5> [[<strong>Priced:</strong> ((price))<br>]] [[<strong>Beds:</strong> ((beds))]][[<strong> Baths:</strong> ((baths))]]<br> [[<strong>SQ FT:</strong> ((squareFeet))<br>]] [[<strong>Garages:</strong> ((garages))]] </h5> </div> <div class="horizontal_results_col03"> <h5> [[<strong>Status: </strong>((status))<br>]] [[<strong>Agent: </strong>((agentId))<br>]] [[<strong>PH: </strong>((phone))<br>]] [[<a href="mailto:((email))">Email Agent</a></h5>]] </div> </div><!-- end of horizontal_results_right --> </div> <!-- end horizontal_results_content --> <div id="horizontal_results_edge"><a href="living_invdetail.php?item=((id))">View Detail</a></div> </div> Here is the code for the first preg_replace_callback (insertDummy) function insertDummy($matches){ $clean = $matches[1]; return "(({\$data['" . $matches[1] . "']}))";} Here is the string after this preg_replace_callback is used <div class="horizontal_results" id="horizontal_results"> <div class="horizontal_results_top"></div> <div class="horizontal_results_content"> <div class="horizontal_results_col01"> <img src="(({$data['photo']}))" alt="" width="150" height="113" class="left" style="margin-left: 5px;"> </div> <div class="horizontal_results_right"> <h4>(({$data['address']}))</h4> <div class="horizontal_results_col02"> <h5> [[<strong>Priced:</strong> (({$data['price']}))<br>]] [[<strong>Beds:</strong> (({$data['beds']}))]][[<strong> Baths:</strong> (({$data['baths']}))]]<br> [[<strong>SQ FT:</strong> (({$data['squareFeet']}))<br>]] [[<strong>Garages:</strong> (({$data['garages']}))]] </h5> </div> <div class="horizontal_results_col03"> <h5> [[<strong>Status: </strong>(({$data['status']}))<br>]] [[<strong>Agent: </strong>(({$data['agentId']}))<br>]] [[<strong>PH: </strong>(({$data['phone']}))<br>]] [[<a href="mailto:(({$data['email']}))">Email Agent</a></h5>]] </div> </div><!-- end of horizontal_results_right --> </div> <!-- end horizontal_results_content --> <div id="horizontal_results_edge"><a href="living_invdetail.php?item=(({$data['id']}))">View Detail</a></div> </div> And after eval is used:(disregard double square brackets. they are for later use) <div class="horizontal_results" id="horizontal_results"> <div class="horizontal_results_top"></div> <div class="horizontal_results_content"> <div class="horizontal_results_col01"> <img src="(({$data['photo']}))" alt="" width="150" height="113" class="left" style="margin-left: 5px;"> </div> <div class="horizontal_results_right"> <h4>(({$data['address']}))</h4> <div class="horizontal_results_col02"> <h5> [[<strong>Priced:</strong> (({$data['price']}))<br>]] [[<strong>Beds:</strong> (({$data['beds']}))]][[<strong> Baths:</strong> (({$data['baths']}))]]<br> [[<strong>SQ FT:</strong> (({$data['squareFeet']}))<br>]] [[<strong>Garages:</strong> (({$data['garages']}))]] </h5> </div> <div class="horizontal_results_col03"> <h5> [[<strong>Status: </strong>(({$data['status']}))<br>]] [[<strong>Agent: </strong>(({$data['agentId']}))<br>]] [[<strong>PH: </strong>(({$data['phone']}))<br>]] [[<a href="mailto:(({$data['email']}))">Email Agent</a></h5>]] </div> </div><!-- end of horizontal_results_right --> </div> <!-- end horizontal_results_content --> <div id="horizontal_results_edge"><a href="living_invdetail.php?item=(({$data['id']}))">View Detail</a></div> </div> As you can see. Nothing changed. Here is what the test does: Insert: $test = "(({\$data['address']}))"; Before: (({$data['address']})) After: ((asdf)) Link to comment Share on other sites More sharing options...
justsomeguy Posted September 22, 2011 Share Posted September 22, 2011 There's a quote problem with eval. Why don't you just directly replace the text with the value from $data instead of using eval? Link to comment Share on other sites More sharing options...
MrFish Posted September 22, 2011 Author Share Posted September 22, 2011 I did end up doing that in the end but I thought it would be quicker for the script to use eval then do yet another str_replace. Link to comment Share on other sites More sharing options...
justsomeguy Posted September 22, 2011 Share Posted September 22, 2011 Eval has a reputation, but it's definitely not because it's fast and efficient. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.