Page 1 of 1
str_replace & concatenate
Posted: Wed Feb 08, 2006 3:39 pm
by kirk7880
How do you replace a string and then concatenate additional information?
The code below performs the string replace. I just need to know how to concatenate info the the replaced string
$buffer = str_replace('"/wiki/', '"' . $pathfromroot . '/spelling/query_term-', $buffer);
Posted: Tue Feb 14, 2006 6:51 pm
by RobertGonzalez
PLEASE USE PHP TAGS AND PHP BBCODE.
Replacing a string:
Code: Select all
<?php
$old_string = 'ThisOldThing';
$new_string = 'BrightShinyThing';
$container = 'IHaveThisOldThing';
//Make a nicer sentence
$container = str_replace($old_string, $new_string, $container);
echo $container;
// This outputs 'IHaveBrightShinyThing'
?>
Concatenating is done like this:
Code: Select all
<?php
$old_string = 'ThisOldThing';
$new_string = 'BrightShinyThing';
$new_string = $old_string . ' stinks compared to ' . $new_string;
echo $new_string;
//This outputs 'ThisOldThing stinks compared to BrightShinyThing'
?>
In your example you should be able to concatenate anywhere near the str_replace call:
Code: Select all
<?php
$buffer = 'ConcatBefore' . str_replace('"/wiki/', '"' . $pathfromroot . '/spelling/query_term-', $buffer) . 'ConcatAfter';
?>
Posted: Wed Feb 15, 2006 2:41 am
by Chris Corbyn
I think the OP wanted to concatentate onto the result, but all in one statement....
Code: Select all
$buffer = preg_replace('/(.*?)wiki(.*)/', '$1$2'.$pathfromroot.'/spelling/query_term-', $buffer);
Posted: Wed Feb 15, 2006 10:58 am
by RobertGonzalez
Yeah, I wasn't sure so I started with the easiest idea first. Thanks for following up.