使用PHP

时间:2015-12-15 16:49:15

标签: php regex

我正在寻找一种方法来返回$search $pattern $length$subject = "hello my name is inigo montoya you killed my father please call me at eight zero zero five five five one to three four prepare to die" $search = array("zero", "one", "two", "to", "too" "three", "four", "five", "six", "seven", "eight", "nine") $length = 10; $result = jedi_find_trick($subject,$search,$length); 的任意组合的一系列(可能)非连续匹配模式的起始位置和匹配模式

在我的示例中,查找数字为单词的电话号码。

$result

$result[0]["start"] = 70 $result[0]["match"] = "eight zero zero five five five one to three four" $result[1] ... 设置为数组:

$search

生成$subject = 'hello my name is inigo montoya you killed my father please call me at eight zero zero five five five one to three four or too oh five seven seven seven five one one one prepare to die'; $search = array('zero','oh','one','two','too','to','three','four','five','six','seven','eight','nine','hundred','thousand'); $replace = array('0','0','1','2','2','2','3','4','5','6','7','8','9','00','000'); $length = 10; $result = jedi_find_trick($subject,$search,$replace,10); $result = jedi_find_trick($subject,$search,$replace,$length); print_r($result); function jedi_find_trick($subject,$search,$replace,$length) { preg_match_all('/(\h*(' . implode('|', $search) . ')\h*){10}/', $subject, $numbers); foreach($numbers[0] as $match) { $number = str_replace($search,$replace,$match); $number = str_replace(' ', '', $number); $number = ' ' . $number . ' '; $subject = str_replace($match,$number,$subject); } return $subject; } 的所有可能组合是我要去的地方,但我觉得有一个更优雅的解决方案逃避我,谢谢你的任何建议。

基于@ chris85的建议,这似乎是一个很好的起点:

hello my name is inigo montoya you killed my father please call me at 8005551234 or 2057775111 prepare to die

返回:

str_replace()

$search“太”需要在“{”之前preg_replace()或者以“2o”结尾。尊重 <initParams path="/update/**,/query,/select,/tvrh,/elevate,/spell"> <lst name="defaults"> <str name="df">allText</str> </lst> </initParams> 的一些词边界应该清理它。

1 个答案:

答案 0 :(得分:1)

这样的事情:

$subject = 'hello my name is inigo montoya you killed my father please call me '
         . 'at eight zero zero five five five one to three four prepare to die';

$search = ['zero', 'one', 'two', 'to', 'too', 'three', 'four', 'five', 'six',
           'seven', 'eight', 'nine'];

$length = 10;

function jedi_find_trick($search, $subject, $length, $sep = ' ', $septype = 0) {
    // quote special characters in the search list
    $search = array_map(function ($i) { return preg_quote($i, '~'); }, $search);
    // quote the separator when it is a literal string
    if ($septype === 0) $sep = preg_quote($sep, '~');

    // build the pattern
    $altern = '(?:' . implode('|', $search) . ')';

    $format = '~(?:%1$s|\A)(%2$s'
            . ($length<2 ? '': '(?:%1$s%2$s){%3$d}')
            . ')(?=%1$s|\z)~';

    $pattern = sprintf($format, $sep, $altern, $length - 1);

    if (preg_match_all($pattern, $subject, $matches, PREG_OFFSET_CAPTURE))
        return $matches[1];

    // return an empty array if there is no match
    return [];
}

print_r(jedi_find_trick($search, $subject, $length));
print_r(jedi_find_trick($search, $subject, 8, '\h+', 1));

默认情况下,分隔符是一个空格。当septype不为0时,表示必须将分隔符视为子模式(因此不需要转义特殊字符)。