str_replace characters with 2 other characters [duplicate] - php

This question already has answers here:
Search and replace multiple values with multiple/different values in PHP5?
(5 answers)
Closed 7 years ago.
I am trying to replace , with . and ' with `
I can do it like this
$bodyweight = str_replace("'", "`", $form->data['bodyweight']);
$bodyweight = str_replace(',', '.', $bodyweight);
Is the a way to do it in one str_replace?

Just read the documentation:
$bodyweight = str_replace(array("'", ','), array("`", '.'), $form->data['bodyweight']);

Related

How can i replace a portion of string starting from the next 4 characters? [duplicate]

This question already has answers here:
How to mask parts of a string with the asterisk character
(5 answers)
Replacing last x amount of numbers
(6 answers)
How to replace string with asterisk(*) using strlen?
(6 answers)
Mask credit card number in PHP
(12 answers)
How can I hide/replace a part of a string in PHP? [duplicate]
(1 answer)
Closed 11 months ago.
I have the below string:
$string = 005390326548;
How can i get this result?
0053****6548
Basically i want to replace starting from the 4 characters the next 4 characters by ****
one-liner solution.
$string= substr_replace("8487013103", "****", 4, 4);
echo $string;
$string = '005390326548';
$retult = substr($string, 0, 4) . '****' . substr($string, 8);

Find values in string between underscores? [duplicate]

This question already has answers here:
PHP: Split string [duplicate]
(7 answers)
Closed 2 years ago.
I have values stored in a database that look like this
10_wc_asis
I need to isolate the values between the underscores (_) like this:
Value 1 = 10
Value 2 = wc
Value 3 = asis
How can I do that in PHP?
Try explode :
$str = "10_wc_asis";
$pieces = explode("_", $str);
var_dump($pieces)

How to add - in-between strings [duplicate]

This question already has answers here:
Insert string at specified position
(11 answers)
Closed 3 years ago.
How do I add - in between strings.
Let’s say for instance, I want to add - in 123456789101 at the fourth position three times thereby making it look like this : 1234-5678-9101.
Substr_replace() or str_replace has not solved the problem.
I would use combination of str_split and implode.
$code = 123456789101;
$formatted = implode('-', str_split($code, 4) );
echo $formatted; //1234-5678-9101
There are a number of ways to do this.
Use substr
$output = sprintf('%s-%s-%s', substr($string, 0,4), substr($string,4,4), substr(8,4));
Use preg_replace
$output = preg_replace('/(.{4,4})(.{4,4})(.{4,4})/', '$1-$2-$3', $string);

How to replace the string in this date format [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
Actual Format 2015-12-16T00:00:00Z
i need like
Result Format: 2015-12-16 00:00:00
i tried this code
$cc=str_replace("T",'',2015-12-16T00:00:00Z);
Using str_replace:
$cc = str_replace('Z', '', str_replace('T', ' ', '2015-12-16T00:00:00Z'));
OR
using preg_replace:
$cc = preg_replace(array('/T/', '/Z/'), array(' ', ''), '2015-12-16T00:00:00Z');

PHP ltrim() not working as expected [duplicate]

This question already has answers here:
PHP ltrim behavior with character list
(2 answers)
Closed 8 years ago.
I have this code..
$homepage1 = 'datastring=/mac_project/portfolio/kitchen/images/03.jpg';
$trimmed = ltrim($homepage1, 'datastring=/mac_project');
echo $trimmed;
I get the output as folio/kitchen/images/03.jpg. It's missing the /port from the /portfolio directory.
Full output should've been /portfolio/kitchen/images/03.jpg
Why not do the simple str_replace() ?
$homepage1 = 'datastring=/mac_project/portfolio/kitchen/images/03.jpg';
$trimmed = str_replace('datastring=/mac_project','',$homepage1);
echo $trimmed;// "prints" /portfolio/kitchen/images/03.jpg
The second parameter for ltrim is for character_mask, which means all the chars in the list will be trimmed.
You could use str_replace(), or if you want to replace only at the beginning of the string by preg_replace():
$trimmed = preg_replace('~^datastring=/mac_project~', '', $homepage1);

Categories