0

I have auto generated string.

One of its elements is a group "/__nh".

Sometimes it is being multiplied, e.g.:

$string = some_stuff_here/__nh/__nh/__nh

I tried code below (and some other variation as well) to replace replace multiple instances with a single one:

return preg_replace('/(//__nh)+/', '/__nh', $string);

.. but I failed (regex is not something I'm comfortable with).

2 Answers 2

2

You should escape your forward slash using a backslash, not another forward slash:

return preg_replace('/(\/__nh)+/', '/__nh', $string);
Sign up to request clarification or add additional context in comments.

1 Comment

I see, wrong escape used by me - probably this heat wave we have in UK at present could be blamed :) thank you
0

Why do you have a double slash on your regex?

Shouldn't it be a back slash ?

Anyways: a greaat regex reference is: http://weblogtoolscollection.com/regex/regex.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.