这是一个新手问题,但是...
我正在另一个 php 文件中调用一个函数,该文件使用 preg_match 来获取字符串。然后我想使用 substr 来获取该字符串的特定部分,但它不会输出字符串中的任何字符。当我从 preg_match
函数替换变量时,我得到了正确的输出。
这是基本代码:
$title = $stream["song1"]; // From a preg_match in an external php file
echo $title; // Correctly prints the song name, in this case "mySong"
echo substr($title, 0, 1); // Outputs a "<" symbol (why??)
如果我运行上面相同的三行,但硬编码歌曲标题:
$title = "mySong";
echo $title; // Correctly prints the song name, in this case "mySong"
echo substr($title, 0, 1); // Outputs a "m" symbol (correct)
另外,当我检查变量的类型时,$title
,它返回"字符串"。 我确定我正在做一些非常愚蠢的事情...谁能帮忙?
似乎
$title
包含html标签,因此第一个字符将是<
。
使用 htmlentities() 回显完整的输出,然后你应该能够看到你实际要查找的字符串的哪一部分。
echo htmlentities($title);
或者,您可以使用 strip_tags() 简单地从字符串中删除所有 html 标记:
$title = strip_tags($title);
echo substr($title, 0, 1); // should work