Jump to content

[SOLVED] question about perg_replace...just first few instances?


Recommended Posts

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!

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.

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!

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

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.