php substr start position not working

I have a problem here with the php function substr

Let's say I have this string coming from an xml file:

$str = '<?xml version="1.0" encoding="utf-8"?><bookstore id="book-1"><languages default="en">';

What I'm trying to do is I'm trying to extract the second tag from that string, the tag

<bookstore id="book-1">

So what I did was this:

First, I got the string starting with the second tag like so:

$newStr = substr($str, strpos($str, '<bookstore '));

My variable $newStr should now be:

$newStr = '<bookstore id="book-1"><languages default="en">';

Then, using that $newStr variable, I tried to get the tag that I wanted (the second tag, which is now the first tag of my $newStr variable) like this:

$wantedTag = substr($newStr, 0, strpos($newStr, '>');

But the problem I'm having is that Nothing is returned (or maybe empty string is returned) when I do this. But if I replace the 0 by 1 like so:

$wantedTag = substr($newStr, 1, strpos($newStr, '>');

I get my second tag but without the initial '<':

$wantedTag = 'bookstore id="book-1">';

Which makes total sense because the character 'b' is at position 1. Why am I not getting anything (or empty string) when I put the start position of the substr function to 0? Any help will be very appreciated.

Please don't tell me to use any of those XML parsers out there like SimpleXML and stuff, this has nothing to do with XML Parsing, it's just basic string manipulation techniques.


I take it you're looking at the result in your browser? The browser will of course interpret any HTML/XML tags, so unless the tag has some content, you won't see anything of it. The string processing itself seems to work fine, see

Need Your Help

Build proguard from it's source code

java build proguard

While using proguard from inside Eclipse on my Unity3d application, I get an error that is caused by corrupt StackMapTable attributes. I found that this is a known issue, whose workaround is to tell

yahoo's attribute exchange -> blank data is coming

php openid yahoo-oauth attribute-exchange

I am trying to build openid login system for my website. To do this I used JanRain's php openid library v 2.1.3.