Anti-Moronic Posted August 14, 2009 Share Posted August 14, 2009 Is there a way to replace just the first few matches? Like if I want to replace the first 3 a's in this: abcd abcd abcd abcd to become: bcd bcd bcd abcd How can I do this with preg_replace or otherwise? Thanks! Link to comment https://forums.phpfreaks.com/topic/170251-solved-question-about-perg_replacejust-first-few-instances/ Share on other sites More sharing options...
.josh Posted August 14, 2009 Share Posted August 14, 2009 preg_replace Link to comment https://forums.phpfreaks.com/topic/170251-solved-question-about-perg_replacejust-first-few-instances/#findComment-898079 Share on other sites More sharing options...
Anti-Moronic Posted August 14, 2009 Author Share Posted August 14, 2009 I'm sorry, was I not rude enough to evoke a response? Don't insult by directing me to the manual. That is the first thing I read!! Is there not a way to implement an offset flag for preg_replace? like preg_match? I can't find anything. Maybe would be better to split the string from the last instance of the 3 a's then only perform the preg_replace on the first part, and reconstruct the string. Was just hoping there would be a simpler way. How about: Is there any way to implement an offset or limit to the nl2br function? So it only limits line breaks to a certain amount, like the first 4, then ignores the rest. I think the answer is right in front of me. Sorry for bothering you in the HELP forum. Link to comment https://forums.phpfreaks.com/topic/170251-solved-question-about-perg_replacejust-first-few-instances/#findComment-898095 Share on other sites More sharing options...
.josh Posted August 14, 2009 Share Posted August 14, 2009 So I guess you missed the part in the manual where you can pass an optional 4th argument to limit the amount of replaces? Link to comment https://forums.phpfreaks.com/topic/170251-solved-question-about-perg_replacejust-first-few-instances/#findComment-898114 Share on other sites More sharing options...
Anti-Moronic Posted August 14, 2009 Author Share Posted August 14, 2009 Not entirely so, I don't always want to replace the first occurrences - I may also want to replace the last occurrences by limit, or maybe random occurrences. I'm sorry, didn't mean to be forward, but not everything can be solved with a one word answer. Thanks anyway! Link to comment https://forums.phpfreaks.com/topic/170251-solved-question-about-perg_replacejust-first-few-instances/#findComment-898144 Share on other sites More sharing options...
.josh Posted August 14, 2009 Share Posted August 14, 2009 Not entirely so, I don't always want to replace the first occurrences - I may also want to replace the last occurrences by limit, or maybe random occurrences. You are right. The limit will not be enough in those situations. But that's not what you said in your OP so don't get upset at me for your failure to be more specific. I'm sorry, didn't mean to be forward, but not everything can be solved with a one word answer. 42 Link to comment https://forums.phpfreaks.com/topic/170251-solved-question-about-perg_replacejust-first-few-instances/#findComment-898171 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.