0

I am currently trying to build a cURL to automatically log in to a test account at a certain website I am building. So far, so good - it logs me in and displays the first account page after the login sequence.

What I would like to do, is the following:

Retrieve the html code of that page, find a certain part of it and set it as a session variable.

The part in question is:

<span class="welcome-user"><span>Welcome, Lakumba Lekendi</span><a href="/signout?signOutMessage">Sign Out</a></span>

Any pointers on how I can achieve that? "Lakumba Lekendi" will not be a constant, since it will change for every user that logs in to their account. I literally wanna find the starting span tag ( class="welcome-user" ) and isolate all of its contents till the closing span tag.

I suppose I can just grab the file contents via file_get_contents, but how do I find that specific piece of code in there, and isolate it properly to set it as a session variable?

lakumba
  • 19
  • 1
  • 5
  • if i understand correctly, you mean the HTML markup? use HTML parsers for that, once you got the HTML string you can use `DOMDocument` – Kevin Mar 19 '15 at 07:35
  • Tons of methods: http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php – n-dru Mar 19 '15 at 07:37
  • Is the DOM extension enabled by default in PHP? – lakumba Mar 19 '15 at 07:43
  • Thank you, guys. Haven't used DOM myself so far, but I'm gonna read up on it. I see I can use DOMDocument::getElementsByTagName, but how do I copy a specific part, starting from a specific span tag, up till the closing span tag? I don't need all the span tags, obviously. – lakumba Mar 19 '15 at 07:50
  • This is not a good practice as you can always do this at the time of printing in html, then why do you need reverse process? However, if you need it, you can use `Regex` for it. – Apul Gupta Mar 19 '15 at 08:08

1 Answers1

0

If you don't want to learn all those dedicated methods, you can always do exploding until you get it:

$h = '<span class="welcome-user"><span>Welcome, Lakumba Lekendi</span><a href="/signout?signOutMessage">Sign Out</a></span>';
$a = explode('<span class="welcome-user"><span>',$h);
$a = $a[1];
$a = explode('</span><a href="/signout',$a);
$a = $a[0];
echo $a;

Gives:

Welcome, Lakumba Lekendi

n-dru
  • 9,285
  • 2
  • 29
  • 42
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/73357/discussion-on-answer-by-n-dru-retrieve-part-of-html-code-of-page-and-set-it-as-a). – Taryn Mar 19 '15 at 14:17