douchen2011 2016-07-15 20:38
浏览 10
已采纳

在字符串中查找控制台转义码的位置

tl;dr How do I write strpos($haystack, '^[[H^[[2J') in PHP?

A linux command line app delivers a screen-full of data followed by a console escape code to return the cursor to the "home" position in a regular loop. To access the data I piped the output to my own script like so:

$ ./otherapp | php myscript.php

It's a continuous stream of data so I use this example for non-blocking stream consumption.

Now to decode the output I need to grab a full frame/screen full of data. The escape codes are shown in nano as ^[[H and ^[[2J. The easiest way seems to be detecting these escape codes and using the output between them.

How are the escape codes represented in a PHP string? Can I use strpos (or mbstring equivalent) to detect their position?

  • 写回答

1条回答 默认 最新

  • dongpi9494 2016-07-15 20:53
    关注

    It seems you're on a Unix environment. To achieve what your escape codes do, you can try with echo -en "\033[H" and echo -en "\033[2J", so I guess you should try something like strpos('\033[H\033[2J', $haystack).

    Maybe you'll need to add an extra backslash ('\\\033[H\\\033{2J').

    Since \033 means "the byte with octal value 33", which is ESC, this should work:

    strpos("\e[H\e[2J", $haystack)
    

    You need to enclose it in double-quotes (") if you want PHP to interpret escape sequences for special characters.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站