TehKittyCat Posted September 6, 2009 Share Posted September 6, 2009 Hi, I am trying to write a regex to add a % to the end of a line and then remove it later on. So basically if a line starts with % add a % to the end of the line. %ifdef DEFINE display: none; %else color: blue; %endif } %ifdef DEFINE% display: none; %else% color: blue; %endif% } Link to comment https://forums.phpfreaks.com/topic/173316-solved-line-starts-with-regex/ Share on other sites More sharing options...
Daniel0 Posted September 6, 2009 Share Posted September 6, 2009 Try this: $text = preg_replace('#^%([^%]+)$#', '%$1%', $text); Edit: Updated regex. Link to comment https://forums.phpfreaks.com/topic/173316-solved-line-starts-with-regex/#findComment-913608 Share on other sites More sharing options...
TehKittyCat Posted September 6, 2009 Author Share Posted September 6, 2009 Ok, I found something that works: $text = preg_replace('/^%.*/m', '$0%', $text); Thanks for the help. Link to comment https://forums.phpfreaks.com/topic/173316-solved-line-starts-with-regex/#findComment-913612 Share on other sites More sharing options...
RussellReal Posted September 6, 2009 Share Posted September 6, 2009 You should probably use Daniel's RegEx because his makes sure there already isn't a % at the other side.. Link to comment https://forums.phpfreaks.com/topic/173316-solved-line-starts-with-regex/#findComment-913733 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.