2012-06-09 66 views
0

这有点菜鸟问题上的字符串变量返回不正确的字符,但是......PHP SUBSTR从功能的preg_match

我打电话使用的preg_match得到一个字符串的另一个PHP文件的功能。然后我想使用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的类型是,它返回“字符串”。我确信我正在做一些非常愚蠢的事情......任何人都可以帮忙吗?

+0

尝试查看$流[ “松1”]的编码。也许它包含多字节字符串,你需要使用mb_substr。试试strlen($ stream [“song1”]); –

回答

2

看来$title包含html标记,因此第一个字符将是<

使用htmlentities()来回显完整输出,然后您应该能够看到您实际正在查找的字符串的哪一部分。

echo htmlentities($title); 

或者,你可以简单地使用strip_tags()字符串中删除所有的HTML标签:

$title = strip_tags($title); 
echo substr($title, 0, 1); // should work 
+0

太棒了,工作。谢谢你的帮助! –