gacon13 Posted August 4, 2008 Share Posted August 4, 2008 I have many string which has single quote string. e.g: abc 'deg' tyui 'tr23' fgh dfgd f sdfg df gdf dfg dfg ... What I want to do is a function that search through the text then find everywhere that has single quote string and replace with a pre-define string, in above e.g i want to replace 'deg' with 'MY_PRE_DEFINE' and 'tr23' with 'MY_PRE_DEFINE' but I have not found the solution yet. Please help. Thanks in advanced. Quote Link to comment https://forums.phpfreaks.com/topic/118056-replace-all-single-quote-string/ Share on other sites More sharing options...
JasonLewis Posted August 4, 2008 Share Posted August 4, 2008 Here try this out: <?php $str = "abc 'deg' tyui 'tr23' fgh dfgd f sdfg df gdf dfg dfg ..."; $search = array( "#\'deg\'#", "#\'tr23\'#", ); $replace = array( "DEG_REPLACER_HERE", "TR23_REPLACER_HERE", ); $str = preg_replace($search,$replace,$str); echo $str; ?> Quote Link to comment https://forums.phpfreaks.com/topic/118056-replace-all-single-quote-string/#findComment-607379 Share on other sites More sharing options...
gacon13 Posted August 5, 2008 Author Share Posted August 5, 2008 Thank you for your reply. But the string in the quote is unknown. I mean the input text is changed and i will not know what is in the quote. For example: text1 = "But the 'string in' the quote is unknown. 'I mean' the input text is changed and i will not know what is in the quote." text2 = "But the string in the quote 'is unknown'. I mean the input text is changed and i will not know 'what is in the' quote." Then whatever the input is, I want to find all things that is quoted by single quote and replace with 'MY_PRE_DEFINE' The output will be: text1 = "But the 'MY_PRE_DEFINE' the quote is unknown. 'MY_PRE_DEFINE' the input text is changed and i will not know what is in the quote." text2 = "But the string in the quote 'MY_PRE_DEFINE'. I mean the input text is changed and i will not know 'MY_PRE_DEFINE' quote." Quote Link to comment https://forums.phpfreaks.com/topic/118056-replace-all-single-quote-string/#findComment-608215 Share on other sites More sharing options...
JasonLewis Posted August 5, 2008 Share Posted August 5, 2008 Like this? <?php $text1 = "But the 'string in' the quote is unknown. 'I mean' the input text is changed and i will not know what is in the quote."; $text2 = "But the string in the quote 'is unknown'. I mean the input text is changed and i will not know 'what is in the' quote."; $text1 = preg_replace("#\'(.*?)\'#","'MY_PRE_DEFINE'",$text1); echo $text1; ?> Quote Link to comment https://forums.phpfreaks.com/topic/118056-replace-all-single-quote-string/#findComment-608385 Share on other sites More sharing options...
gacon13 Posted August 5, 2008 Author Share Posted August 5, 2008 Thank you a lot. I still do not understand preg_replace pattern, I use $pattern = "#'.*?'#s"; the result is the same. Now I'm trying to find what has been replaced, e.g: MY_PRE_DEFINE = tr23 I saved the input text into 2 files then do a search to find what could help to find the different and found that php diff can do. But I just want to return the array of the input and output string only, not the whole content. How could I do that? Quote Link to comment https://forums.phpfreaks.com/topic/118056-replace-all-single-quote-string/#findComment-608408 Share on other sites More sharing options...
effigy Posted August 5, 2008 Share Posted August 5, 2008 <pre> <?php $data = <<<DATA But the 'string in' the quote is unknown. 'I mean' the input text is changed and i will not know what is in the quote. But the string in the quote 'is unknown'. I mean the input text is changed and i will not know 'what is in the' quote. DATA; $finds = array(); $data = preg_replace("/'([^']*)'/e", " ### The ternary is only used in order to pull off ### the arraying and the replacing. I couldn't get ### it to work any other way. array_push(\$finds, '$1') ? 'MY_PRE_DEFINE' : '$1' ; ", $data); echo $data, '<hr>'; print_r($finds); ?> </pre> Quote Link to comment https://forums.phpfreaks.com/topic/118056-replace-all-single-quote-string/#findComment-608668 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.