无法将 ob_get_clean() 字符串转换为整数,表示输出是 18 个字符的字符串

发布于 2024-12-19 05:15:23 字数 619 浏览 1 评论 0原文

我试图从一个仅回显文本而不是返回到变量的函数中获取一个数字,如下所示:

ob_start();
function_to_get_id_number();
$thisIDnumber = ob_get_clean();

如果我回显 $thisIDnumber 变量,则所需的数字将打印在 HTML 输出中。

但是,运行 var_dump($thisIDnumber) 会输出以下内容:string(18) "7" (假设数字是 7,尽管 var_dump() 报告一个 18 个字符的字符串,无论该数字是什么。)

任何将字符串转换为整数的尝试(例如 (int)$thisIDnumber ,或 int_val($thisIDnumber),或 $thisIDnumber = 0+$thisIDnumber 失败且输出为 0)

正在运行mb_detect_encoding($thisIDnumber) 报告字符串是 ASCII 编码的。

我不太确定如何解决这个问题,但非常感谢任何建议或见解!非常感谢!

I am trying to obtain a number from a function which only echos text instead of returning it to a variable as follows:

ob_start();
function_to_get_id_number();
$thisIDnumber = ob_get_clean();

If I echo the $thisIDnumber variable, the desired number is printed in the HTML output.

However, running var_dump($thisIDnumber) outputs the following: string(18) "7"
(Assuming the number was 7, although var_dump() reports an 18-character string regardless of what the number might be.)

Any attempt to convert the string to an integer (e.g. (int)$thisIDnumber , or int_val($thisIDnumber), or $thisIDnumber = 0+$thisIDnumber fails and the output is 0)

Running mb_detect_encoding($thisIDnumber) reports the string to be ASCII encoded.

I'm not really sure how to get around this, but would very greatly appreciate any suggestions or insights! Many thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

我不会写诗 2024-12-26 05:15:23

如果该函数打印大量空白(空格、制表符甚至回车符)并且您通过网络浏览器检查 var_dump() 的输出(因此它会呈现),则可以解释 string(18) 部分因为 HTML 和空格已折叠)。但是,转换为数字应该忽略常规的前导空格。所以可能还有其他一些不可打印的字符。

试试这个:

ob_start();
function_to_get_id_number();
$thisIDnumber = ob_get_clean();

var_dump($thisIDnumber, bin2hex($thisIDnumber));

十六进制代码应该可以让您了解字符串内部的内容。

更新:

$data = '5b777073635f63617465676f72795f69645d';

for($i=0, $len=strlen($data); $i<$len; $i+=2){
    echo chr(hexdec(substr($data, $i, 2)));
}

...打印以下内容::

[wpsc_category_id]

-?

The string(18) part could be explained if the function prints lots of white space (spaces, tabs or even carriage returns) and you inspect var_dump()'s output through a web browser (so it renders as HTML and spaces are collapsed). However, casting to number should ignore regular leading spaces. So there's possibly some other non-printable character out there.

Try this:

ob_start();
function_to_get_id_number();
$thisIDnumber = ob_get_clean();

var_dump($thisIDnumber, bin2hex($thisIDnumber));

The hexadecimal code should give you a clue of what's inside the string.

Update:

$data = '5b777073635f63617465676f72795f69645d';

for($i=0, $len=strlen($data); $i<$len; $i+=2){
    echo chr(hexdec(substr($data, $i, 2)));
}

... prints this:

[wpsc_category_id]

:-?

記柔刀 2024-12-26 05:15:23

您没有提供太多可供使用的信息,因此这是一个盲目的尝试。

你为什么不试试这个:
如果 id 根据定义是数字,请通过使用正则表达式删除其他所有内容来强制执行。

(正如 jproffit 在对您的问题的评论中所写:有关 function_to_get_id_number() 的更多信息会很好。为什么您需要首先缓冲输出?不能 function_to_.. . 返回正确的值而不是输出字符串?)

You have not provided much information to work with, so this is a shot in the dark.

Why don't you try this:
If the id is numeric by definition, enforce it by removing everything else using a regular expression.

(As jproffit wrote in his comment to your question: more info on function_to_get_id_number() would be nice. Why do you need to buffer the output in the first place? Can't function_to_... return a proper value instead of outputting a string?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文