views:

74

answers:

4

I need to replace a word with another word. But

$var= str_replace($linklabel[$k], $linklabelmod[$k], $var);

is not giving the desired result. For example i have a string

$var="the theory of them thesis"; 
$linklabel[1]="the"; 
$linklabelmod[1]="hhh";

What i need is, i just want to replace the word "the". But since "the" is repeated in "theory" "thesis" and "them", all those starting three letters are also getting replaced. Then output becomes $var="hhh hhhory of hhhm hhhsis";//wrong But i need the output $var="hhh theory of them thesis";//write I am bad at explaining a question, plz excuse me...

Thanks in advance....


From what I (paxdiablo) can gather from the OP's comments, this is the code following modifications (still claimed not to work):

foreach($xpath->query('//a') as $element) {
    $linklabel[] = $element->textContent;
    $link[] = $element->getAttribute("href");
    $i=$i+1;
}
for($k=0;$k<$i;$k++) {
    $linklabelmod[$k] = str_replace($linklabel[$k], $linklabel[$k]."[$k]", $linklabel[$k]);
    $var = preg_replace ('/\b'.preg_quote($linklabel[$k]).'\b/', $linklabelmod[$k], $var);
}
print $var; //printing web page
+4  A: 

The normal way of doing what you want is to use a regular expression replace with word boundaries:

$var = preg_replace ('/\bthe\b/', 'hhh', $var);

or:

$var = preg_replace ('/\b'.preg_quote($linklabel[$k]).'\b/', $linklabelmod[$k], $var);
paxdiablo
plz , can u give me an example. I am new to php reg expressions...Thx
Kiran George
$var = preg_replace( "\b$linklabel[$k]\b", $linklabelmod[$k], $var, 1 ); // is this u a telling, once again, i am totally new to preg_replace
Kiran George
You are missing delimiters I think ;)
Felix Kling
preg_replace('/\b'.$linklabel[$k].'\b/i',$linklabelmod[$k], $var);
RobertPitt
You have also forgot to use preg_qoute as your matching this way.
RobertPitt
Oops, thanks, Felix and Robert.
paxdiablo
excuse me, this is not working for me. I am noob and i don't know y. It is still replacing all the matching letters from the words...This is the codeforeach($xpath->query('//a') as $element) { $linklabel[] = $element->textContent; $link[] = $element->getAttribute("href"); $i=$i+1;}for($k=0;$k<$i;$k++) { $linklabelmod[$k] = str_replace($linklabel[$k], $linklabel[$k]."[$k]", $linklabel[$k]);$var = preg_replace ('/\b'.preg_quote($linklabel[$k]).'\b/', $linklabelmod[$k], $var);}print $var;//printing web page
Kiran George
@paxdiablo , thx. i sorted it out finally
Kiran George
A: 
$var = "the theory of them thesis"; 
$linklabel[1] = "the"; 
$linklabelmod[1] = "hhh";

$var = str_replace( " " . $linklabel[1] . " ", 
                    " " . $linklabelmod[1] . " ", 
                    " " . $var . " ");
$var = trim($var);
galambalazs
this also not working. Here is my code **foreach($xpath->query('//a') as $element) { $linklabel[] = $element->textContent; $link[] = $element->getAttribute("href"); $i=$i+1;}for($k=0;$k<$i;$k++) { $linklabelmod[$k] = str_replace($linklabel[$k], $linklabel[$k]."[$k]", $linklabel[$k]); $var = str_replace( " " . $linklabel[$k] . " ", " " . $linklabelmod[$k] . " ", " " . $var . " ");}print $var;//printing web page**
Kiran George
excuse me, may be i am a noob, but it is not woring for me
Kiran George
paste the exact code to a new php file, and you will get: "hhh theory of them thesis". Everything else beyond that is a problem in your code.
galambalazs
The code reported in the answer works as expected. @Kiran George: Both this solution, and the one using `preg_replace()` work; if they are not working in your case, then you should report the exact code you are using; people don't have the crystal ball to debug your code.
kiamlaluno
+2  A: 

Well i would always advise in using str_replace over preg_replace but in this case you might have to.

<?php

$k = 1;
$var="the theory of them thesis";
$linklabel[1]="the"; 
$linklabelmod[1]="hhh";

$var = preg_replace('/\b'.preg_quote($linklabel[$k]).'\b/i',$linklabelmod[$k], $var);

?>

Dont forget to preg_qoute the text to minimize the amount of errors

RobertPitt
nope, this is not giving the resultWarning: preg_replace() [function.preg-replace]: Unknown modifier 'p' in C:\wamp\www\test\f.php on line 21Warning: preg_replace() [function.preg-replace]: Unknown modifier '0' in C:\wamp\www\test\f.php on line 21
Kiran George
Is this using the preg_quote version iv'e recently updated ?
RobertPitt
yea sorry, speedy fingers get away from me!
RobertPitt
Fatal error: Call to undefined function preg_qoute() in C:\wamp\www\test\f.php on line 20 .. How to define preg_qoute();
Kiran George
i made a spelling mistake, try now :)
RobertPitt
Now its showing Warning: preg_replace() [function.preg-replace]: Unknown modifier 'p' in C:\wamp\www\test\f.php on line 20Warning: preg_replace() [function.preg-replace]: Unknown modifier '0' in C:\wamp\www\test\f.php on line 20
Kiran George
I tried the version without `preg_quote()`, and I didn't get any error messages. @Kiran George: If you are using the function with the same exact code, then you should not get such errors (where is the `p` modifier in a string like `/\bthe\b/i`?). You would probably get the right answer if you show the actual code you are using.
kiamlaluno
thank you .....
Kiran George
Also the preg_qoute will modify the text your finding so it will auto escape any special chars that may effect the regex string.
RobertPitt
+3  A: 

Insert white space :) if you want to use str_replace function

php > $var = "the theory of them thesis";

php > $var = str_replace(array(" the ", "the "), 'hhh', $var);

php > echo $var;

ivan73
You also have to add white spaces to `'hhh'`.
Felix Kling
Right, thank you :)
ivan73
Although this will work for the string given this will also change words ending in `the` to hhh too. EG `lathe ` would be replaced as `lahhh` as well as ignoring any `the` preceding any punctuation. So while it will work for this particular instance (`hhh` space issue notwithstanding) it's flawed as a general pattern.
The problem is that the example given from the OP is not related with the real code he needs to change.
kiamlaluno