Is there a way to only replace 1 instance of a string within a string. For instance:
$string = "The house is made of bricks bricks.";
Is there a way to only take out the first "bricks" within that string. Please bare in mind that the string could be anything, of any length.
Thanks.
[SOLVED] Replace only one instance
Moderator: General Moderators
Code: Select all
<?php
$replacement = 'bricks';
$string = 'The house is made of bricks bricks.';
$string = substr_replace($string, '', strpos($string, $replacement), strlen($replacement));
echo $string;
?>You could explode() your string and compare each "substring" against the next. If they are not the same then add the compared one to your output string. Something like
But this could be very time and resorces consuming. Even useless...
Code: Select all
function removeSame($jono){
jonoArray=explode($jono);
$outPutString=""
for($i=0; $i<count($jonoArray)-1;$i++){
if($jonoArrayї$i] != $jonoArrayї$i+1]){
$outPutString .=$jonoArrayї$i] . " ";
}
return $outPutString;
}