Jump to content

Replace Disp "Text" with "Text"_ using preg_replace


Recommended Posts

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

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.

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.