Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/248.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP替换字符串中的表达式_Php - Fatal编程技术网

PHP替换字符串中的表达式

PHP替换字符串中的表达式,php,Php,我试图自己用PHP解决这个问题,但我还没有运气。 我有这样的字符串: “你好,我是考试12:30,等等” "11:54" “你好,我是考试23:00” “00:10,这是另一个文本” 我想把这部分文本HH:MM替换为这段文本:“new”,所以结果应该是: “你好,我是一个测试新的和布拉布拉布拉布拉布拉布拉布拉” “新的” “你好,我是新来的” “新的,这是另一个文本” 并获取数组中所有提取的文本,如下所示: “你好,我是考试12:30,等等” 结果=[12:30] "11:54" 结果=[11:

我试图自己用PHP解决这个问题,但我还没有运气。 我有这样的字符串:

“你好,我是考试12:30,等等” "11:54" “你好,我是考试23:00” “00:10,这是另一个文本”

我想把这部分文本HH:MM替换为这段文本:“new”,所以结果应该是: “你好,我是一个测试新的和布拉布拉布拉布拉布拉布拉布拉” “新的” “你好,我是新来的” “新的,这是另一个文本”

并获取数组中所有提取的文本,如下所示: “你好,我是考试12:30,等等” 结果=[12:30] "11:54" 结果=[11:54] “你好,我是考试23:00” 结果=[23:00] “00:10,这是另一个文本” 结果=[00:10] “你好,我是23:00考试,还有一个07:55” 结果=[23:00,07:55]

注意:字符串可以包含多个HH:MM,例如: “你好,我是23:00考试,还有一个07:55”

我不喜欢使用正则表达式。 谢谢大家!

检查这个

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

如果你不想使用正则表达式。。。但是…我不确定普通php是否更容易理解。事实上,你应该避免这样做:

    <?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 the@repl 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";
?>

这回答了你的问题吗?“我不喜欢使用正则表达式”-为什么?(考虑到这些可能是最适合您工作的工具。)这是因为我看不到reg表达式在做什么,简单的php代码很容易逐行理解,无论如何,我愿意使用它,“我只是不喜欢使用它”。如果您有一个使用reg exp的解决方案,请展示它,这将是非常受欢迎的!谢谢。像这样的网站在学习正则表达式时非常有用。谢谢@PavelJanicek,但这个答案是针对“1-23不带前导零”。这不是我的情况,我的情况是HH:MM更少的字符
\d\d:\d\d
谢谢Dilraj,但是有办法提取HH:MM的值吗?我的意思是提取一个包含所有找到值的数组,比如12:30,23:00,etcHi Fernando,你可以使用preg_match_all方法,例如var_dump(preg_match_all('/[\d]{0,2}:[\d]{0,2}/',“你好,我是测试12:30和bla 01:02 bla bla bla bla”,$matches),$matches),$matches);谢谢你,迪拉杰!非常好的解决方案!无论如何,我想使用其他没有正则表达式的解决方案,但这只是我个人的选择,谢谢!谢谢你@Mondo,看起来不错!我们能得到一个包含所有提取的HH:MM的数组吗?像这样:result=[23:00,07:55]$parties持有它。所以substr($pieces,strlen($pieces)-5)应该包含它。注意:我没有测试。谢谢@Mondo,它工作得很好!仅供参考此脚本最多只支持2个表达式,例如不支持:“你好14:30我是测试23:00,还有一个07:55”。非常感谢。