在第一次出现像(1-9)这样的数字之前,我试图删除字符串中的所有数据可能是在函数中吗?
示例:
$value = removeEverythingBefore($value, '1-10');
所以如果我有一个像#34的测试那么你好我想在100个小时左右的时间里统治这个世界"
我希望这能找到第一个出现的1
数字并删除它之前的所有内容。
留下100 hours or so
。
答案 0 :(得分:0)
您可以使用preg_replace
这样的正则表达式/([a-z\s]*)(?=\d)/i
,如下所示:
$string = "Hello I want to rule the world in 100 hours or so";
$newString = preg_replace("/([a-z\s]*)(?=\d)/i", "", $string);
echo $newString; // Outputs "100 hours or so"
您可以看到它与this eval.in一起使用。如果你想在一个函数中使用它,你可以使用它:
function removeEverythingBeforeNumber($string)
{
return preg_replace("/([a-z\s]*)(?=\d)/i", "", $string);
}
$newString = removeEverythingBeforeNumber("Hello I want to rule the world in 100 hours or so");
答案 1 :(得分:0)
答案 2 :(得分:0)
如果你想调用你在帖子中提到的功能,你可以这样做:
<?php
function removeEverythingBefore($value, $pattern) {
preg_match($pattern, $value, $matches, PREG_OFFSET_CAPTURE);
$initialPosition = $matches[0][1];
return substr($value, $initialPosition);
}
$value = "Hello I want to rule the world in 100 hours or so";
$value = removeEverythingBefore($value, '/[0-9]/');
echo $value; // prints 100 hours or so
这样你就可以使用相同的功能来匹配其他模式。