0

i am currently trying to convert this code:

private static String CutOutXML(string resp)
{
        var url = resp.Substring(resp.IndexOf("<div>") + "<div>".Length);
        url = url.Substring(0, url.IndexOf("</div>"));
        return url;
}

to PHP, but this isn't working:

$startpos = strrpos($correcthtml, '<div>');
$correcthtml = substr($correcthtml, $startpos);
$stoppos = strrpos($correcthtml, '</div>');
$correcthtml = substr($correcthtml, $stoppos);

i am trying to curl a web url from the internet and i only need one xml tag out of it. I am now trying for so long and i dont come behind it how to do this.

7
  • Does this answer your question? PHP: Regular Expression to get a URL from a string Commented Jun 8, 2021 at 12:06
  • This can get very complicated quickly. What does $correcthtml contain? A full website? A single, simple XML string? Commented Jun 8, 2021 at 12:09
  • No a Full Website, but the html tags are set correctly and in C# this works Commented Jun 8, 2021 at 12:12
  • How is it "not working"? Unexpected result? No result? Error? Commented Jun 8, 2021 at 12:15
  • Mind that in the C# version you add the length of <div> to the position for first substring. Commented Jun 8, 2021 at 12:16

1 Answer 1

1

the translation of

private static String CutOutXML(string resp)
{
    var url = resp.Substring(resp.IndexOf("<div>") + "<div>".Length);
    url = url.Substring(0, url.IndexOf("</div>"));
    return url;
}

would be

function CutOutXML(string $resp):string
{
    $url = substr($resp,strrpos($resp,'<div>')+ strlen('<div>'));
    $url = substr($url,0, strrpos($url,"</div>"));
    return $url;
}

however as the other comments say there are much better ways of doing what you appear to be trying to do

eg

$matches = [];
preg_match('/<div>(.*)<\/div>/', $resp, $matches);
print_r($matches);
Sign up to request clarification or add additional context in comments.

3 Comments

php uses both ' and " to delimit string ' is for absolute string " for string that allow interpolation as neither string includes a $ then shouldn't make a difference
Ok, thx. Was just wondering because one time ' , one time ". But alrighty then.
in c# ' would be @"" and " would be $""

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.