如何在php中将“abcde fghijk”这样的字符串变成“* abcde * * fghijk *”? [关闭]

I have a string eg abcde fghijk and am trying to convert it into *abcdef* *fghijk*and another thing is that it should work for any value that can be entered in place of abcde fghijk

http://php.net/manual/fr/function.str-replace.php

$value = str_replace("abcde", "*abcde*", " abcde fghijk ");
$value = str_replace("fghijk", "*fghijk*", $value);
<?php
$string = 'abcde fghijk lmnopq';
$array = explode(' ', $string);
$array = array_map(function($v){return "*".$v."*";}, $array);
$string = implode(' ', $array);
var_dump($string);

result:

kris-roofe@krisroofe-Rev-station:~$ php cal.php 
string(25) "*abcde* *fghijk* *lmnopq*"

I suppose this is pretty much what you're expecting:

$str = ' abcde fghijk ';
$str = preg_replace('/(\w+)/', '*$1*', $str);
echo $str;