Reputation: 15459
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...
Upvotes: 1
Views: 1139
Reputation: 1397
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];
Upvotes: 4
Reputation: 47894
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);
Upvotes: 0
Reputation: 1819
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."
Upvotes: 3
Reputation: 2569
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.
Upvotes: -2