Str replace only full words??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
djkoe
Forum Newbie
Posts: 2
Joined: Fri May 21, 2004 2:30 am

Str replace only full words??

Post 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:
User avatar
Wayne
Forum Contributor
Posts: 339
Joined: Wed Jun 05, 2002 10:59 am

Post by Wayne »

try using

Code: Select all

$bericht = ereg_replace("/\b$naam\b/i","<a hRef='detail.php?id=$id'>$naam</a>",$bericht);
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

add a space infront and behind the text to replace..
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post 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.
djkoe
Forum Newbie
Posts: 2
Joined: Fri May 21, 2004 2:30 am

Post 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);
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post 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.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

Then write the regex. I still haven't found a good tutorial, so I can't, I'm just giving ideas.
Post Reply