pastet89;10900070 wrote:Hi all! I have a difficult task I have to solve. If somebody can help me, I will be very thankful to him.
$string = "Ab1cDSb2cWFb3cL";
$desired_string = "A5DS5WF5L";
How can I get $desired_string, having just $string
$string = "Ab1cDSb2cWFb3cL";
$desired_string = preg_replace('#[a-z]#', '', str_replace('b', '5', preg_replace('#[0-9]#', '', $string)));
echo $desired_string; // result: A5DS5WF5L
How can I replace all the places in $string containg "aSOMETHING-UNKNOWNb" with "5"? 😕
$string = 'aQuick Brown Fox Jumped Over The Fenceb';
$desired_string = preg_replace('#a.*b#', '5', $string);
echo $desired_string; // result: 5
If you want to replace aSOMETHING-UNKNOWNb which is inside a longer string, you could use:
$string = 'Today, I am asecretly bestowing someoneb awesome.';
$desired_string = preg_replace('#\ba.*?b\b#', '5', $string);
echo $desired_string; // result: Today, I 5 awesome.
As you can see, there is a 'gotcha', with words like 'am' which can kick-start the matching for replacement.. but if this is not an issue, it should work otherwise as expected...