preg_match help please

Hi Guys!

I am using CURL to retrieve the results from a page and on that page is 2 hidden form fields. What I need to do is get the value of the hidden form fields. So basically what I need is some preg_match code to get the value from the following fields:

<input type="hidden" name="cvref" value="3575469">
<input type="hidden" name="filekey" value="jZDV9dr8DETkD1s4">

So what I need to do is extract the values from the above fields. In short I need the following to be returned from the preg_match:



There are much better ways to accomplish the task like the others have said. But here’s the regular expression to use: (tell me if it doesn’t work, I might have messed up on the quotes)

// Assuming $html_code is the HTML of the entire page

$cvref = preg_replace('/^.*name\\="cvref"\\s+value\\="(.+)"\\>.*$/', '$1', $html_code);

$filekey =  preg_replace('/^.*name\\="filekey"\\s+value\\="(.+)"\\>.*$/', '$1', $html_code);

No, code is not working…there are lots of these errors (probably about 20 or more on the page).

As it is a warning, you maybe able to safely ignore it. Does the rest of the code function as normal…?

Why would you think that? The fact you’re using curl is irrelevant, you’re performing operations on the ‘string’ returned. Namely $page_1_result . :wink:

Read the documentation, it should clear things up for you.

Okay I see. I tried your suggestion, but im getting the following error when trying to load the values of $page_result_1:

Warning: DOMDocument::loadHTML() [function.DOMDocument-loadHTML]: htmlParseEntityRef: no name in Entity, line: 522 in /home/myuser/public_html/ on line 104

Of-course DOM is the best way to achieve what you are trying to do.
Ignore all those warning or put @ in front of loadHTML() to suppress all warnings.

echo the two variables $foo and $bar in the end.

If you dont get any values try without xpath, check this. HTML Parsing in PHP.

I’m going to play the little devil (or evil leprechaun) on your opposite shoulder to Anthony.

Sound advice, but I might go so far as to say that Zaggs is not trying to parse a structured document; in the sense that one might need a tool to provide a programmatic interface to a whole document. Consider the following, almost identical but different in a slight yet meaningful way, snippet based on the Zaggs’ HTML snippet.

[input type="hidden" name="cvref" value="3575469"]
[input type="hidden" name="filekey" value="jZDV9dr8DETkD1s4"]

See what I did?

Off Topic:

Normally, I would too be offering a DOM—(or other more suitable tool)—based answer but the bandwagon is getting tired and old. Those bright young whipper-snappers who, frequenting the places where such topics are raised, so confidently bemoan the practicality of regexes within 50 paces of the acronym HTML are readily becoming quite wearisome.

P.S. If Zaggs likes the look of using the DOM extension to get at his required values, I’ll be happy to sidle up to Anthony to help arrive at a nice solution.

I am using CURL() because im posting data to another website on a different server. So I guess, I can’t use your method?

Don’t (or at least don’t default to it) use RegExp to parse a structured document, there are tools out there better suited to it.

See [fphp]DOMDocument[/fphp] and [fphp]DOMXPath[/fphp] .

$html = '
    <form action="" method="post">
      <input type="hidden" name="foo" value="foo-value" />
      <input type="hidden" name="bar" value="bar-value" />

$doc = new DOMDocument;

$xpath = new DOMXPath($doc);

$foo = $xpath->query("//input[@type='hidden' and @name='foo']/@value")->item(0)->nodeValue; #foo-value
$bar = $xpath->query("//input[@type='hidden' and @name='bar']/@value")->item(0)->nodeValue; #bar-value


I’m using CURL() like this and it’s returning a string:

$curl_handle = curl_init();
			// Set the URL to be processed
			curl_setopt($curl_handle, CURLOPT_URL, "");
			// Assign the data to be sent
			curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $data);
			// Return details
			curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
			// Set result var
			$page_1_result = curl_exec($curl_handle);

Is there a reason you’re not using the DOM to read the elements required?