1

Take for example these strings:

  $strOne = "Place new content here:  , but not past the commma.";
  $strTwo = "test content";

So based on the strings above, how do make a new string that looks like this:

  $finalstr = "Place new content here:  test content, but not past the comma.";

EDIT

Also, lets assume I don't have access to $strOne, meaning I want to modify it via string functions not directly the string via concatenation etc...

2
  • 2
    Have you tried something ? Commented Sep 11, 2015 at 19:01
  • not sure what functions php has to do this.. Commented Sep 11, 2015 at 19:04

5 Answers 5

4

You can split the first string by comma and then concat in the way you want. To split you may use explode method:

$strArray = explode(',', $strOne, 2);
$finalstr = $strArray[0].$strTwo.",".$strArray[1];
Sign up to request clarification or add additional context in comments.

Comments

3

Try a combination of strpos and substr_replace ?

$strOne = "Place new content here:  , but not past the commma.";
$strTwo = "test content";

// find the position of comma first
$pos = strpos($strOne, ',');
if ($pos !== false)
{
     // insert the new string at the position of the comma
     $newstr = substr_replace($strOne, $strTwo, $pos, 0);
     var_dump($newstr);
}

Output:

string(63) "Place new content here: test content, but not past the commma."

Comments

0

str_replace() does not have the capability to limit the number of replacements. preg_replace() though not necesary for finding your needle in the haystack string DOES allow replacement limiting. Using explode() is not a horrible approach, but you should limit the explosions to 2 if you use it. I do not like the indirect-ness of generating a temporary array when a string result is sought.

Code: (Demo)

$strOne = "Place new content here:  , but not past the commma.";
$strTwo = "test content";

echo preg_replace('/,/', "$strTwo,", $strOne, 1);

You could use preg_replace('/(?=,)/', $strTwo, $strOne, 1) to avoid mentioning the needle in the replacement string, but that regex will perform worse because the lookahead will be performed on every step while traversing the haystring string -- not great.

If your needle string might contain characters that have special meaning to the regex engine, then use '/' . preg_quote($yourNeedle, '/') . '/' as your pattern.


If your needle is more variable, then use the same regex technique. If wanting to find the first hashtag substring in a block of text, merely search for the first occurring # followed by one (or more) word character(s). If you want to enforce that the # is preceded by a space, you can add that too. You can use a lookahead to avoid copying the needle into the replacement parameter but this might execute a little slower depending on the quality of the haystack string.

Input:

$strOne = "Programming requires life long learning #learning #stackoverflow #programming";
$strTwo = "\n\n";

Code: (Demo)

echo preg_replace('/#\w/', "$strTwo$0", $strOne, 1);
// or echo preg_replace('/ (#\w)/', "$strTwo$1", $strOne, 1); // to replace the preceding space with 2 newlines

Or: (Demo)

echo preg_replace('/ \K(?=#\w)/', $strTwo, $strOne, 1);

Comments

-2

With your first example:

$strTwo = "test content";
$strOne = "Place new content here: $strTwo, but not past the commma.";

To go further: use an array of string, and create a function which returns the concatenations of the strings.

Comments

-2
$finalstr = str_replace(',', $strTwo.',', $strOne, 1);

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.