Currently I have an array
$cop_row = array('first_pulse1', 'second_pulse2');
what I want is to replace first_ & second_ from the cop_row array .
I am using this right now but it is not giving me the required result.
str_replace("first_","",$cop_row);
I am getting output
pulse1second_pulse2
What I want is
pulse1pulse2
Thanks for your concern.
this will solve your problem.
php > $x = ['first_pulse', 'second_pulse'];
php > $q = preg_replace('/(\w+)_/i', '', $x);
php > print_r($q);
Array
(
[0] => pulse
[1] => pulse
)
php >
You can use preg_replace.
$cop_row = array('first_pulse1', 'second_pulse2');
$patterns[0] = '/first_/';
$patterns[1] = '/second_/';
foreach($cop_row as $row){
echo preg_replace($patterns, '', $row);
}
Try this :
$replace = ["first_","second_"];
$cop_row = array('first_pulse1', 'second_pulse2');
str_replace($replace,"",$cop_row);
Please have a look into https://www.w3schools.com/php/func_string_str_replace.asp
If the strings are always formatted like this, then you could use this basic RegExp replacement:
preg_replace("/^.*_/", "", $cop_row)
The pattern might need some improvements, but it works for your cases.
If you need further assistance or explanation regarding the pattern, feel free to ask!
$cop_row = array('first_pulse1', 'second_pulse2');
foreach ($cop_row as $key => $value) {
$result_array[] = substr($value, strpos($value, "_") + 1);
}
print_r($result_array);
Working example here - http://codepad.org/nOqWdmNu