I'm trying to replace words in PHP using preg_replace. When executing the code, it will replace "fuel cap for car" with "cap", as opposed to "auto parts".
$string = "fuel cap for car";
$find = array('cap', 'fuel cap for car');
$replace = array('hat', 'auto parts');
echo preg_replace($find, $replace, $string);
So, I've already tried a different method. I'm storing the original and replacement values in a database, for example:
Table
Original: automotive|motorcycle|automobile|car parts|piston|motorbike|metal fuel cap for a motorbike Replace: AUTO PARTS
Orignal: cap|baseball cap|hat|beanie Replace: HAT
The string being: fuel cap for car.
The code is:
$result = mysqli_query($connect, $query);
while($row2 = mysqli_fetch_assoc($result)){
if(preg_match('/\b'.$row[suggestions].'\b/i', $string)){
$string = $row['description'];
But, it's replacing the string with "hat", not "auto parts"