函数strlen()不返回正确的长度

时间:2017-07-13 03:13:32

标签: php strlen

我不知道为什么strlen()没有返回正确的字符串长度。实际长度为29,但strlen()返回41。请帮忙。

$str = htmlentities('<td bgcolor=#FFFFFF>2-0</td>');
echo strlen($str);

1 个答案:

答案 0 :(得分:2)

您转义的HTML实体的字符串是:

# Eg. User name="admin", Password="admin" for this code sample.
$user = "admin"
$pass = "noPassword"

# Build auth header
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))

# Set proper headers
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))
$headers.Add('Accept','application/json')
$headers.Add('Content-Type','application/json')

# Specify endpoint uri
$uri = "https://xxxxx.service-now.com/api/now/table/incident"

# Specify HTTP method
$method = "post"

# Specify request body
{request.body ? "$body = \"" :""}}{\"active\":\"true\",\"number\":\"123\",\"short_description\":\"test\"}"

# Send HTTP request
$response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri -Body $body

# Print response
$response.RawContent

此字符串的长度为40个字符(在您的问题中不是41),通过调用&lt;td bgcolor=#FFFFFF&gt;2-0&lt;/td&gt; 正确返回。

在不转义HTML实体的情况下,长度为28(不是29)。

strlen()
相关问题