-1

I have a database too many data and need to replace with some character after character.

Example

his name is wong lei
he buy a remote gold1
remote-code 1221

It should be after code

his name is wong lei
he buy a remote diamond20
remote-code 1221

Meaning i want find remote character and then replace with word i set. every character in data after remote may not same.

  • `str_repace('gold1', 'diamond20', $text)` – Honk der Hase Sep 29 '22 at 06:30
  • can't because it have too many data, word gold1 may be bronze5,gold5,gold90 or something like that. – Khalil Ismail Sep 29 '22 at 06:35
  • And? where is the problem preventing you from _developing_ a solution that uses str_replace? – Honk der Hase Sep 29 '22 at 06:40
  • 1
    Using correct phrasing might be helpful, when you are _researching_ a solution. What you are calling "character", is actually more of a _word_. I would approach this using regular expressions. – CBroe Sep 29 '22 at 06:42
  • Does this answer your question? [Str\_replace for multiple items](https://stackoverflow.com/questions/7605480/str-replace-for-multiple-items) – jspit Sep 29 '22 at 07:04
  • Alternatively you can create an array with `['from' => 'to',..]` and work with `strtr`. https://www.php.net/manual/en/function.strtr.php – jspit Sep 29 '22 at 07:09
  • _"I have a database too many data"_ - Is it the string you get from MySQL you want to change before doing something else with it, or do you want to change the data in the database itself? If it's the data in your database you want to change, you should rather look into how to make an UPDATE query that replaces the values you want to change. Something like this: https://stackoverflow.com/a/14586441/2453432 or if you need to do it using regex: https://stackoverflow.com/a/26179601/2453432 – M. Eriksson Sep 29 '22 at 10:39

1 Answers1

2

sorry i just found my own solution by myself.

//create goto new line function explode
function explodenewline($string){
    return explode("\n",$string);
}

//create go to after space function explode
function explodes($string){
    return explode(" ",$string);
}

when in while (from many data), create variable for get in line two

$line2 = explodenewline($fromwholetext)[1];

get word/character from 4th position

$character = explodes($line2)[4];

then replace it

str_replace($character, 'diamond20', $fromwholetext);