dr5779 2017-11-26 15:04
浏览 68

PHP为什么preg_match结果与正常输出不同?

This is first php code

$bbtext = "<br/><br/>This [color=red]is [b]bold[/b] and this is [u]underlined[/u] and [color=green]this [/color]is in [i]italics[/i] with[/color] a [color=red] red color[/color]";
preg_match_all('/\[color=(.*?)\]/', $bbtext, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);

the above php code output is, Output 1

Then, other php code

$pattern = '/abcd/';
$string = 'weruhfabcdwuir';
preg_match($pattern, $string, $matches, PREG_OFFSET_CAPTURE);
print_r($matches);

output is, Output 2

In second php code, abcd is start from 6th index position. (we know that index always starts from 0). It is working correctly on php output.

In first php code, [color=red] is start from 5th index position. but, output is shown as 15.

why this difference occurred on output 1?

  • 写回答

1条回答 默认 最新

  • dongtangu6144 2017-11-26 15:09
    关注

    I'm not sure why you think it starts at character offset 5, 15 looks right to me:

    <br/><br/>This [color=red]is [b]bold[/b] and this is [u]underlined[/u] and [color=green]this [/color]is in [i]italics[/i] with[/color] a [color=red] red color[/color]";
                   ^ character offset 15
    

    Are we missing something?

    评论

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思