Jump to content

Help with simple(ish) preg_match script


sungo

Recommended Posts

Hey guys, I'm new to PHP and am trying to teach myself out of books / off tutorial websites, and I've run into a problem with something I am trying to make.

 

I'm basically trying to make a translation widget that you can enter data into and it will feed that data through a web-based translator (on a different site), parse the data, and feed it back to you.

 

I made a form, that passes the information to the server via cURL and parses the data via regex(/preg_match_all). Sometimes, when you search, there are too many words and you get a "More results" button. I can also grab this data from the server but I cannot seem to make the script parse the web data you get from hitting the button. It merely just loads the host servers website and doesn't re-parse the data. If I added more preg_match_all/regex things, I could strip away the things I want but then it would have to be infinitely deep code for however many pages deep the "More results" goes with the code in the current form. I figure there's a way around this?

 

I'm sure you can do something with a function, to make this work correctly but I'm new so I'm failing hard. I've tried a million different things. If anyone can give me some tips, it'd be much appreciated. I could use the instruction. Thanks!

 

You can check out a the form here

 

 

My apologizes ahead of time for ugly scripting, but here it is thusfar :

 


<?php

// Site is in Japanese EUC encoding, so need a meta content-type
echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=EUC-JP\" />";
// Establish a form for the possible "More Results" later
echo "<FORM NAME=\"inp\" ID=\"inp\" ACTION=\"process2.php\" METHOD=\"POST\" >";

// Parsing POST data variables from the form
$dsrchkey = $_POST["dsrchkey"];
$dicsel = $_POST["dicsel"];
$dsrchtype = $_POST["dsrchtype"];
$engpri = $_POST["engpri"];
$exactm = $_POST["exactm"];
$firstkanj = $_POST["firstkanj"];

// Perparing POST variables for cURL
if ($dsrchtype=="J") {
	$dsrchtype_on="&dsrchtype=$dsrchtype";
	}
	else {
		$dsrchtype_on="";
	}
if ($engpri=="X") {
	$engpri_on="&engpri=$engpri";
	}
	else {
		$engpri_on="";
		}
if ($exactm=="X") {
	$exactm_on="&exactm=$exactm";
	}
	else {
		$exactm_on="";
		}
if ($firstkanj=="X") {
	$firstkanj_on="&firstkanj=$firstkanj";
	}
	else {
		$firstkanj="";
		}


// cURL for grabbing the data
$post_data = "dsrchkey=$dsrchkey&dicsel=$dicsel".$dsrchtype_on.$engpri_on.$exactm_on.$firstkanj;
$post = $post_data
$url = "http://www.csse.monash.edu.au/~jwb/cgi-bin/wwwjdic.cgi?1E";
$ch = curl_init($url);
   	curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$page = curl_exec($ch);

// if you click the "More Results" button below, cURL the data from a new url
if (isset($_POST['Action'])) {

	$actionparam = $_POST['actionparam'];
	$post_data = "actionparam=".$actionparam;
	$url = "http://www.csse.monash.edu.au/~jwb/cgi-bin/wwwjdic.cgi?1F";
	$post = $post_data;
	$ch = curl_init($url);
   		curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

	echo curl_exec($ch);

	} else {

		}	

// Regex to grab appropriate data
preg_match_all('/<p>(.+?)<input type="hidden" name="actionparam" value="/s', $page, $match1);
	// Grabs the variable used to point to "More Results"
preg_match_all('/<input type="hidden" name="actionparam" value="(.+?)">/s', $page, $match2);
	// Checks to see if there are "More results"
preg_match_all('/<INPUT TYPE="submit" NAME="Action" VALUE="More(.+?)">/s', $page, $match3);

$match_no = $match2[1][0];
$more_match = $match3[1][0];

// Echos Regex'd data
echo $match1[1][0];

// If more results exist, make a button to go to the more results!
if ( $more_match == "" ) {

	} else {

		echo "<input type=\"hidden\" name=\"actionparam\" value=\"".$match_no."\">";
		echo "<INPUT TYPE=\"submit\" NAME=\"Action\" VALUE=\"More results\">";

	}

echo "</form>";

?>

 

Ideally I'd like to make the script self-contained, meaning it all runs from one php, or two, but I really have limited experience with how to integrate like that.

 

Thanks again!!  :)

 

 

Link to comment
https://forums.phpfreaks.com/topic/154504-help-with-simpleish-preg_match-script/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.