1

There's a code in the software that I'm modifying, which looks <?php echo $str;> that have an output of http://old.example.com/12345-title.html and when I tried to modify the string with this https://stackoverflow.com/a/1252710/2007055 technique it didn't work..

And I find out when I do echo "TEST" . $str; it returns a different output which looks like TEST-0/posts/11-cat/22-sub-cat/12345-title.html but returns "http://old.example.com/12345-title.htmlTEST" when I do echo $str . "TEST";..

My question is how can I modify that kind of a string? From "http://old.example.com/12345-title.html" into "http://www.example.com/12345-title.html" ?

Community
  • 1
  • 1

1 Answers1

0

Use this ~http://(.*?)\.~

<?php
$str="http://old.example.com/12345-title.html";
echo $str = preg_replace("~http://(.*?)\.~","http://www.", $str);

OUTPUT :

http://www.example.com/12345-title.html

enter image description here

Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • You don't need to store a new string in a new variable, the string that have a different value when you echoed it with a prefix, is already stored in a variable, on the software.. – 5ervant - techintel.github.io Apr 01 '14 at 08:15
  • Nothing happened, the output is still `http://old.example.com/12345-title.html`.. Maybe because the `preg_replace` cannot find the value that's being searched for, because the subject string contains a different value in the other side. – 5ervant - techintel.github.io Apr 01 '14 at 09:00
  • Nothing happened ? Here's the demo of the same above code. https://eval.in/130065 It works as it should. Maybe should provide a list of inputs on your question. – Shankar Narayana Damodaran Apr 01 '14 at 09:21
  • Yes I know that your solution and my solution will work on any regular string.. But not to this one, because please considered when I tried to do `echo "TEST" . $str;` it returns a different value which is `TEST-0/posts/11-cat/22-sub-cat/12345-title.html` instead of `TESThttp://old.example.com/12345-title.html`. – 5ervant - techintel.github.io Apr 01 '14 at 11:12