1

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.

5 Answers 5

2

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 > 

http://php.net/manual/en/function.preg-match.php

Sign up to request clarification or add additional context in comments.

Comments

2

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!

Comments

2
$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

Comments

1

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); 
}

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.