-1

I have a variable ($uid) which fetches value from a database. My problem is that I am not able to append that variable to another variable ($str). The value of the $uid will be in place of '___' in the $str variable. How do I do approach?

$uid = 5;
$str ='ALPHA|___|NA|2|NA|NA|NA|INR|NA|NA|BETA|NA|NA|NA|NA|NA|NA|NA|NA|NA|NA';
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 2
    Read about `str_replace();` `echo str_replace('___',$uid,$str);` – JYoThI Jul 03 '18 at 05:43
  • str_replace('__', $uid, $str); – Parth Shah Jul 03 '18 at 05:43
  • 4
    Did you do _any_ research at all before asking? I'm pretty sure a simple search for "replace a string in PHP" would have given you an answer faster than it took you to write this question. – M. Eriksson Jul 03 '18 at 05:44
  • Possible duplicate of [How replace variable in string with value in php?](https://stackoverflow.com/questions/15065387/how-replace-variable-in-string-with-value-in-php) – nageen nayak Jul 03 '18 at 05:46
  • If you changed the quotes - you could use `$str ="ALPHA|$uid|NA|..."`, just make sure you use double quotes rather than single quotes. – Nigel Ren Jul 03 '18 at 05:49

2 Answers2

0
//use this code 
$uid = 5; $str ='ALPHA|___|NA|2|NA|NA|NA|INR|NA|NA|BETA|NA|NA|NA|NA|NA|NA|NA|NA|NA|NA';
echo str_replace("___",$uid,$str);
Ved
  • 716
  • 4
  • 8
0

try this $uid = 5;

$str ='ALPHA|___|NA|2|NA|NA|NA|INR|NA|NA|BETA|NA|NA|NA|NA|NA|NA|NA|NA|NA|NA';

echo str_replace("___",$uid,$str);

output : ALPHA|5|NA|2|NA|NA|NA|INR|NA|NA|BETA|NA|NA|NA|NA|NA|NA|NA|NA|NA|NA

Chirag
  • 363
  • 2
  • 12
  • That is ok but my main purpose is to calculate checksums for the $str variable with the $uid variable in place of ___. I don't want to output the value – Apha219 Jul 03 '18 at 05:58