RE: [PHP] Is there a way to use the strpos() for next string...
| From: | Ford, Mike [LSS] | Date: | Thu, 20 Nov 2003 17:54:45 +0000 |
| Subject: | RE: [PHP] Is there a way to use the strpos() for next string... | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-170458@lists.php.net to get a copy of this message | ||
On 20 November 2003 17:39, Scott Fletcher wrote:
> How exactly does the 3rd parameter option work. I tried this
> but it doesn't
> work, so I don't know how exactly does it work... There isn't detail
> information on the php.net website...
>
> --snip--
> $XML_Start = (strpos($res_str,"<![CDATA[",1)+9);
> $HTML_Start = (strpos($res_str,"<![CDATA[",2)+9);
> $HTML_End = strpos($res_str,"]]>",1);
> $XML_End = strpos($res_str,"]]>",2);
> --snip--
The manual is perfectly clear -- the third parameter is the offset within the string of where to
start searching, not an occurrence number or any other way of counting. And the return value of
strpos() is also an offset within the string. So, assuming your CDATA segments are nested (which
the above seems to imply), then:
> $XML_Start = strpos($res_str, "<![CDATA[") + 9;
> $HTML_Start = strpos($res_str,"<![CDATA[", $XML_Start) + 9;
> $HTML_End = strpos($res_str, "]]>", $HTML_Start);
> $XML_End = strpos($res_str, "]]>", $HTML_End+3);
If they're not nested, then the search order would need to be different (you need to look for
substrings in the order in which they occur in the string as a whole) but the principle is exactly
the same.
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services,
JG125, James Graham Building, Leeds Metropolitan University,
Beckett Park, LEEDS, LS6 3QS, United Kingdom
Email: m.ford@leedsmet.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211