0

Example: (UPDATED - corrected the code below)

<?php

$canonical = 'http://eg.com/in/sample-post/';

$pos = strpos( $canonical, 'in' );

echo substr_replace( $canonical, 'main', $pos, strlen('in') );

// OUTPUT: http://eg.com/mamain/sample-post/

?>

As shown in the example, I am trying to replace the first instance of uk (subject) in the URL with main (replacement). Instead the output shows mamain. I am not sure why.

But the same code seems to work if the no. of characters in the subject is greater than replacement, e.g.

<?php

$canonical = 'http://eg.com/main/sample-post/';

$pos = strpos( $canonical, 'main' );

echo substr_replace( $canonical, 'uk', $pos, strlen('main') );

// OUTPUT: http://eg.com/uk/sample-post/

?>

Clearly, the second code although the same, does what I intend it to do.

How do I modify the code so that it works in both instances? What logic am I missing here?

NOTE:

  • The code is only to give you an idea of what I am doing.

  • I'd like to avoid having to use preg_replace to avoid regex complications.

3
  • 1
    The first example is working fine for me, I get the output http://eg.com/main/sample-post/ as intended. Are you sure that there is no other error in your original code? Commented Dec 28, 2013 at 16:18
  • @BluePsyduck You are right. Corrected the code. You should be able to reproduce the error now. Commented Dec 28, 2013 at 16:23
  • Nope, I still get the same output. But now it looks like you are calling the function holding this code twice, so you replace the in to main, and then (by accident) again the in in main to mamain. Can you ensure that this code is only executed once on your variable? Commented Dec 28, 2013 at 16:27

3 Answers 3

1

Your examples are working fine for me.

But it looks like you are calling the function holding this code twice, so you replace the in to main, and then (by accident) again the in in main to mamain.

Can you ensure that this code is only executed once on your variable?

Sign up to request clarification or add additional context in comments.

Comments

0

You can use preg_replace here:

$canonical = 'http://eg.com/uk/sample-post/';
echo preg_replace('/\buk\b/', 'main', $canonical) . "\n";

$canonical = 'http://eg.com/main/sample-post/';
echo preg_replace('/\bmain\b/', 'uk', $canonical) . "\n";

OUTPUT:

http://eg.com/main/sample-post/
http://eg.com/uk/sample-post/

3 Comments

The reason why I am using substr_replace is to avoid the regex complications that come with preg_replace. :-/
Solutions should be driven by requirement. You requirement is such that a simple preg_replace can solve it without much complication.
The code shown in my question is only a sample of what I am doing (which is more dynamic). But thanks for your time. :)
0

It seems to work for me:

$string = 'http://eg.com/uk/sample-post/';

echo substr_replace( $string, 'main', strpos($string,'uk'), strlen('uk'));

You could also use:

$string = 'http://eg.com/uk/sample-post/';

echo str_replace('uk','main', $string);

But this will replace all occurrences...

1 Comment

Jason, I had edited my question just before you posted your answer. Please take a look at the updated code (first code block) in my question.

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.