arunpatal Posted June 14, 2014 Share Posted June 14, 2014 Hello everyone, example string <?php $string = ""; $string .= "<textarea id='convert'> 'AA' </textarea>"; $string .= "<textarea id='convert'> 'BB' </textarea>"; $string .= "<textarea id='not_convert'> 'AA' </textarea>"; $string .= "<textarea id='not_convert'> 'DD' </textarea>"; ?> I want to change 'AA' to 'ZZ' which is inside textarea with the id convert only. How is it possible with the help of regular expression? Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted June 14, 2014 Share Posted June 14, 2014 If the textareas always look the same, you could use str_replace(): http://www.php.net//manual/en/function.str-replace.php Quote Link to comment Share on other sites More sharing options...
arunpatal Posted June 14, 2014 Author Share Posted June 14, 2014 (edited) If the textareas always look the same, you could use str_replace(): http://www.php.net//manual/en/function.str-replace.php str_replace() will not solve the problem... i can change textarea a bit if its requeired but lets say if string is like this and i want to change all AA to ZZ <?php $string = ""; $string .= '<textarea id="convert"> "AA" KAA="AA" $AA</textarea>'; $string .= "<textarea id='convert'> 'BB' </textarea>"; $string .= "<textarea id='not_convert'> 'AA' </textarea>"; $string .= "<textarea id='not_convert'> 'DD' </textarea>"; ?> Edited June 14, 2014 by arunpatal Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted June 14, 2014 Share Posted June 14, 2014 Could try this if your using PHP 5.3.0 or newer $string = preg_replace_callback('/(<textarea id="convert">)(.*?)(<\/textarea>)/is' ,function($m) { array_shift($m); $m[1] = str_replace('AA', 'ZZ', $m[1]); return implode($m); } ,$string); echo $string; Quote Link to comment Share on other sites More sharing options...
arunpatal Posted June 14, 2014 Author Share Posted June 14, 2014 Could try this if your using PHP 5.3.0 or newer $string = preg_replace_callback('/(<textarea id="convert">)(.*?)(<\/textarea>)/is' ,function($m) { array_shift($m); $m[1] = str_replace('AA', 'ZZ', $m[1]); return implode($m); } ,$string); echo $string; Sorry but how do i implement this code with the above code i wrote? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted June 14, 2014 Share Posted June 14, 2014 After you have defined the $string variable. Quote Link to comment Share on other sites More sharing options...
arunpatal Posted June 14, 2014 Author Share Posted June 14, 2014 After you have defined the $string variable. $string = ""; $string .= "<textarea id='convert'> 'AA' </textarea>"; $string .= "<textarea id='convert'> 'BB' </textarea>"; $string .= "<textarea id='not_convert'> 'CC' </textarea>"; $string .= "<textarea id='not_convert'> 'DD' </textarea>"; $string = preg_replace_callback('/(<textarea id="convert">)(.*?)(<\/textarea>)/is' ,function($m) { array_shift($m); $m[1] = str_replace('AA', 'ZZ', $m[1]); return implode($m); } ,$string); echo $string; I tried this way but its not working.... Quote Link to comment Share on other sites More sharing options...
Solution Ch0cu3r Posted June 14, 2014 Solution Share Posted June 14, 2014 Because you are using single quotes for denoting the textarea id. Change your single quotes to double quotes instead $string = ""; $string .= "<textarea id=\"convert\"> 'AA' </textarea>"; $string .= "<textarea id=\"convert\"> 'BB' </textarea>"; $string .= "<textarea id=\"not_convert\"> 'CC' </textarea>"; $string .= "<textarea id=\"not_convert\"> 'DD' </textarea>"; Quote Link to comment Share on other sites More sharing options...
arunpatal Posted June 14, 2014 Author Share Posted June 14, 2014 Because you are using single quotes for denoting the textarea id. Change your single quotes to double quotes instead $string = ""; $string .= "<textarea id=\"convert\"> 'AA' </textarea>"; $string .= "<textarea id=\"convert\"> 'BB' </textarea>"; $string .= "<textarea id=\"not_convert\"> 'CC' </textarea>"; $string .= "<textarea id=\"not_convert\"> 'DD' </textarea>"; Thanks.. its working Quote Link to comment 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.