Home » Php » PHP replace expression in string

PHP replace expression in string

Posted by: admin February 25, 2020 Leave a comment

Questions:

I was trying to get this solved in PHP by my self but I had no luck yet.
I have strings like these:

“hello I am a test 12:30 and bla bla bla”
“11:54”
“hello I am a test 23:00”
“00:10 and this is another text”

And I would like to replace this part of the text HH:MM with this text: “new”, so the result should be:
“hello I am a test new and bla bla bla”
“new”
“hello I am a test new”
“new and this is another text”

And also get all the extracted texts in an array, like this:
“hello I am a test 12:30 and bla bla bla”
result=[12:30] “11:54”
result=[11:54] “hello I am a test 23:00”
result=[23:00] “00:10 and this is another text”
result=[00:10] “hello I am a test 23:00 and there is one more 07:55”
result=[23:00,07:55]

NOTE: the string can contains more than one HH:MM, for example:
“hello I am a test 23:00 and there is one more 07:55”

I prefer not use regular expressions.
Thank you!

How to&Answers:

check this

var_dump(preg_replace('/[\d]{0,2}:[\d]{0,2}/',"new","hello I am a test 12:30 and bla bla bla"));

Answer:

If you don’t want use regex… but…i’m not sure that plain php it’s more easy to understan or mantain. Indeed you should avoid this:

    <?php

/*
   Since the couple HH:MM have a fixed pattern and a fixed length
   search for it in the string and replace each occurrence of them with the @repl 
   you desire.
   NOTE: Each block (HH:MM) Have total length of five char.
   @str : The string on which perform the search.
   @repl : The replacement string
   @return : A new string with each HH:MM occurrence replaced with
    the values of [email protected] param
 */
function findEachHM($str, $repl) {
    $refined_str = "";
    $lo = 0; # Last occurrence of the delimiter
    $li = 0; # Last portion taken of the main string
    $delim = ":";

    while (strpos($str, $delim, $lo)) {
        # Take the whole block
        $lo = strpos($str, $delim, $lo) + 3;
        # take the pieces of string that goes from the last occurence of delim to another
        $pieces = substr($str, $li, $lo);
        # doing the replace
        $refined_str .= str_replace(substr($pieces, strlen($pieces) - 5), $repl, $pieces);
        $li = $lo;
    }
    return $refined_str;
}

$str = "hello I am a test 23:00 and there is one more 07:55";
$repl = "new";
$rs = findEachHM($str, $repl);
echo $rs . "\n";
?>

This should work for multiple hour:

function findEachHM($str, $repl) {
    $refined_str = array();
    $lo = 0; # Last occurrence of the delimiter
    $li = 0; # Last portion taken of the main string
    $delim = ":";
    $all_delim = array();

    while(strpos($str, $delim, $lo)){
      $lo = strpos($str, $delim, $lo)+1;
      array_push($all_delim, $lo);
    }

    foreach($all_delim as $k=>$pos){
      $pieces = substr($str, $li, (($pos-$li)+2));
      array_push($refined_str, str_replace(substr($pieces, strlen($pieces)-5), $repl, $pieces));
      $li = $pos+2;
    }

    return implode("", $refined_str);
}