Page 1 of 1

Str replace only full words??

Posted: Fri May 21, 2004 2:30 am
by djkoe
hi,
i have made a script.
but the thing is that when he replaces words from a database, he also replaces b.v. when the word AI is in the database then in the word Wait he replaces ai automaticly.
he replaces words that are in the database with hyperlink words (and if you click that then you get info about that word).
i only want to let php replace words IF it is a standalone word so:

Code: Select all

the word        Replace?
____________________
AI                  YES
Wait               No
how can i do that?
see the script underneath here:
replace.php

Code: Select all

<?

// the hyperlink function
function vervang ($bericht)
{
    $breezah = FALSE;
    $bericht = nl2br(htmlspecialchars(stripslashes($bericht)));

    // Get info from database and replace words mysql_connect('*****','******','*******'); mysql_select_db('******');
$resultaat=mysql_query("SELECT * FROM lijst;");
while(list($id,$catid,$naam,$info)=mysql_fetch_row
($resultaat)){   
   $bericht = str_replace("$naam","<a hRef='detail.php?id=$id'>$naam</a>",$bericht); $naam2 = strtolower($naam);
   $bericht = str_replace("$naam2","<a hRef='detail.php?id=$id'>$naam</a>",$bericht);

}
// Return the hyperlink and the text
return $bericht;
}


    echo vervang($_POST[bericht]);


    // Form to input text...
    ?>
    <FORM METHOD=POST ACTION="vervang.php">
    <B>Verander deze tekst in een vervang TeKsT:</B><br>
    <TEXTAREA NAME="bericht" ROWS="10" COLS="50"></TEXTAREA><br>
    <INPUT TYPE="submit" value="verander!" name="submit">
    </FORM>
    
    <?



?>
okay some variables:
bericht = message
vervang = replace
verander = change

Please help me! :cry:

Posted: Fri May 21, 2004 4:50 am
by Wayne
try using

Code: Select all

$bericht = ereg_replace("/\b$naam\b/i","<a hRef='detail.php?id=$id'>$naam</a>",$bericht);

Posted: Fri May 21, 2004 8:27 am
by d3ad1ysp0rk
add a space infront and behind the text to replace..

Posted: Fri May 21, 2004 8:57 am
by jason
LiLpunkSkateR wrote:add a space infront and behind the text to replace..
Problem with that is if the word is at the end of a sentence. (Or in Parenthesis), has a comma after it, etc.

Posted: Fri May 21, 2004 4:08 pm
by djkoe
that can be fixed...

Code: Select all

$bericht = ereg_replace(" $naam "," <a hRef='detail.php?id=$id'>$naam</a> ",$bericht);
$bericht = ereg_replace(" $naam."," <a hRef='detail.php?id=$id'>$naam</a>.",$bericht);
$bericht = ereg_replace(" $naam,"," <a hRef='detail.php?id=$id'>$naam</a>,",$bericht);
$bericht = ereg_replace(",$naam ",",<a hRef='detail.php?id=$id'>$naam</a> ",$bericht);

Posted: Fri May 21, 2004 5:06 pm
by d3ad1ysp0rk
Jason:
- Check if it has a space on both sides
- Check if it has a space in the beggining and a piece of punctuation at the end
- Check if it has a space at the end and a random non-alphanumeric character at the beggining.

if any of them are met, replace it.

Posted: Fri May 21, 2004 5:36 pm
by redmonkey
Wayne wrote: try using

Code: Select all

$bericht = ereg_replace("/\b$naam\b/i","<a hRef='detail.php?id=$id'>$naam</a>",$bericht);
Probably the best method but this is not the correct syntax for defining ereg regex patterns, this is a preg regex pattern so should be...

Code: Select all

$bericht = preg_replace("/\b$naam\b/i","<a hRef='detail.php?id=$id'>$naam</a>",$bericht);
djkoe wrote: that can be fixed...

Code: Select all

$bericht = ereg_replace(" $naam "," <a hRef='detail.php?id=$id'>$naam</a> ",$bericht);
$bericht = ereg_replace(" $naam."," <a hRef='detail.php?id=$id'>$naam</a>.",$bericht);
$bericht = ereg_replace(" $naam,"," <a hRef='detail.php?id=$id'>$naam</a>,",$bericht);
$bericht = ereg_replace(",$naam ",",<a hRef='detail.php?id=$id'>$naam</a> ",$bericht);
Sorry, but that is just a completely inefficient use of regex, if you are going to physically hardcode your replacement combinations you should use str_replace as it is quicker. Also as Jason points out, the actual word could be followed by more than just a comma, space or period.
LiLpunkSkateR wrote: Jason:
- Check if it has a space on both sides
- Check if it has a space in the beggining and a piece of punctuation at the end
- Check if it has a space at the end and a random non-alphanumeric character at the beggining.

if any of them are met, replace it.
Again, writing code to specifically check for these various combinations is highly inefficient when a relatively simple regex would do the job.

Posted: Fri May 21, 2004 5:49 pm
by d3ad1ysp0rk
Then write the regex. I still haven't found a good tutorial, so I can't, I'm just giving ideas.