qureshi Posted June 22, 2010 Share Posted June 22, 2010 Hi there, I have been trying to solve this myself, but have had no luck. I have this: Disp "Weight:" Disp X/Y But I want to transform it to: "Weight:"_ X/Y_ Using this: preg_replace("/Disp(.*?)/i","$1_",$string) But it doesn't work. Instead I get: _ "Weight:" _ X/Y I'm running php 5.3 on windows. Latest XAMPP. Thanks in advance, Nadeem Quote Link to comment https://forums.phpfreaks.com/topic/205491-replace-disp-text-with-text_-using-preg_replace/ Share on other sites More sharing options...
premiso Posted June 22, 2010 Share Posted June 22, 2010 preg_replace("/Disp(.*)/i","$1_",$string) Should work. Quote Link to comment https://forums.phpfreaks.com/topic/205491-replace-disp-text-with-text_-using-preg_replace/#findComment-1075396 Share on other sites More sharing options...
qureshi Posted June 22, 2010 Author Share Posted June 22, 2010 Hi there, No change. Same exact result. Regards, Nadeem Quote Link to comment https://forums.phpfreaks.com/topic/205491-replace-disp-text-with-text_-using-preg_replace/#findComment-1075488 Share on other sites More sharing options...
premiso Posted June 22, 2010 Share Posted June 22, 2010 Ran this: <?php $string = 'Disp "Weight:" Disp X/Y'; echo preg_replace("/Disp(.*)/i","$1_",$string); ?> Got this: "Weight:"_ X/Y_ You need to check your code and make sure you are updating the right file etc. Quote Link to comment https://forums.phpfreaks.com/topic/205491-replace-disp-text-with-text_-using-preg_replace/#findComment-1075561 Share on other sites More sharing options...
qureshi Posted June 22, 2010 Author Share Posted June 22, 2010 Hi, Thanks for your code. It works. How do I get the Regex to not include newline as a part of $1 ? Nadeem Quote Link to comment https://forums.phpfreaks.com/topic/205491-replace-disp-text-with-text_-using-preg_replace/#findComment-1075767 Share on other sites More sharing options...
premiso Posted June 22, 2010 Share Posted June 22, 2010 Why not just do a str_replace on the string before or after to remove the newlines? echo str_replace("\n", "", preg_replace("/Disp(.*)/i","$1_",$string)); Simplistic as that. Quote Link to comment https://forums.phpfreaks.com/topic/205491-replace-disp-text-with-text_-using-preg_replace/#findComment-1075790 Share on other sites More sharing options...
salathe Posted June 23, 2010 Share Posted June 23, 2010 Be aware that given the example string, there will be Carriage Return (\r or ASCII 13) characters left floating around since the Windows-style line ending (\r\n) is used. Quote Link to comment https://forums.phpfreaks.com/topic/205491-replace-disp-text-with-text_-using-preg_replace/#findComment-1076017 Share on other sites More sharing options...
premiso Posted June 23, 2010 Share Posted June 23, 2010 Be aware that given the example string, there will be Carriage Return (\r or ASCII 13) characters left floating around since the Windows-style line ending (\r\n) is used. Oh.. echo str_replace(array("\n", "\r"), "", preg_replace("/Disp(.*)/i","$1_",$string)); Should sort it. Quote Link to comment https://forums.phpfreaks.com/topic/205491-replace-disp-text-with-text_-using-preg_replace/#findComment-1076122 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.