Can't get regular expression to work in PHP. Just outputs the word Array
I'm having trou开发者_StackOverflow社区ble getting a regexp in PHP to work. It's supposed to get the string between the tags on a web page, but all I get in return is the word Array when I try to echo it.
This is the text I'm using the regexp on. It's part of a web page downloaded as a string.
<title>
HTC Desire S
</title>
This is the code I'm using for the regular expression.
while(!feof($list_of_phones))
{
$phone = fgets($list_of_phones);
$info = file_get_contents($phone);
preg_match_all("/\/<title>(.*)<\/title>/", $info, $title, PREG_OFFSET_CAPTURE);
echo $title[0];
}
Please read the manual page carefully. The third argument is populated with a multi-dimensional array. You can use var_dump() to inspect your variables.
pre_match_all()
$matches
is always a multdimensional array (usually 2-level). When you set the PREG_OFFSET_CAPTURE
-flag, the result is also one level deeper.
PREG_OFFSET_CAPTURE If this flag is passed, for every occurring match the appendant string offset will also be returned. Note that this changes the value of matches into an array where every element is an array consisting of the matched string at offset 0 and its string offset into subject at offset 1.
echo $title[0][0][0];
For further debugging: Never use echo
, use var_dump()
, or print_r()
instead. This will give you much more information, than echo
.
Then you use this regex
/\/<title>(.*)<\/title>/
As far as I can see this also expects a /
(specified by \/
) right before <title>
. I don't think, that this is, what you want. (When we choose a different delimiter here, its more obvious: ~/<title>(.*)</title>~
)
First problem: Your regex appears to have an extra slash in it:
/\/<title>(.*)<\/title>/
^^
Not sure why you've got that there, but I think it should be this:
/<title>(.*)<\/title>/
Second problem: even when you get that right, $title[0]
will still be an array; you need $title[0][0]
to get the full <title>content</title>
string, and $title[1][0]
to get just the text inside the <title>
tag.
Hope that helps.
Firstly, there appears to be an error in your regular expression - namely the initial \/
.
Secondly, $title[0]
will not contain what you are looking for. Apply a print_r
(or similar) to $title
and you'll see which pieces are relevant.
精彩评论