Jump to content

Get HTML source into a string?


silver8fox2

Recommended Posts

I'm trying to load someone's myspace.com into a string, and pull out specific parts of the html text. It would then seach the string ($html) and pick out the first value $name which would be <span class="nametext">NameHere</span>... and so forth. I then will take the values and place it on a banner for my own personal site...

 

I've tried for the past few hours searching but can't come up with the right words. It's kinda like forum sigs in which they take values from a site and place it in a image.

Link to comment
Share on other sites

Can't remember where I got these functions from and I'm not using them anymore, but I think that they are on the lines that you want :

<?php
function loadTemplate($file)
{
global $template;

$strfile = $file;

        if (!file_exists($strfile)) print"Loading template failed!";
    	$thisfile = file($strfile);

    	while(list($line,$value) = each($thisfile)) {
        	$value = ereg_replace("(\r|\n)","",$value);
        	$template .= "$value\r\n";
    	}

    	return $template;
}

function insertTempData($file, $recordset_array) {
global $template;

$this->loadTemplate($file);

foreach ($recordset_array as $key => $value) {
	$template = str_replace("id='" . $key . "' value=''", "id='" . $key . "' value='" . $value . "'", $template);
}

return $template;
}
?>

 

So what you need to do is pass your file to function loadTemplate($file),

That will turn your file into one big long string called $template.

Then create an array of constants you want to change, then pass them to function insertTempData($file, $recordset_array).

Then do what you want with $template.

 

Hope that helps ;)

Link to comment
Share on other sites

On of my friends back in the day did this, i noticed its slow though but kinda works lol.

 

Prints out:

Username: Mark Ryan

Age: 20 years old

Gender: Male

Friends: 111

 

 

<?php

### Class ###
class sig{

	function parse($start, $end, $string, $is_url = false){

		if($is_url == true){

			$string	=	file_get_contents($string);

		}

		$starting	=	explode($start, $string);
		$ending		=	explode($end, $starting[1]);

		return $ending[0];

	}

	function InStr($string, $find){
		$i	=	0;
		while(strlen($string)>=$i){
			unset($substring);
			$substring	=	substr($string, $i, strlen($find));
			if($substring	==	$find){
				return true;
			}
			$i++;
		}
		return false;
	}

}

### /Class ###
$Sig	=	new Sig;
### /Get Data ###	

$id="144816244";

// Gets HTML Source
$info = file_get_contents("http://profile.myspace.com/index.cfm?fuseaction=user.viewprofile&friendid=$id");

//Gather String Text
$username = ($Sig->parse("<meta name=\"description\" content=\"MySpace Profile - ", ",", $info));
$age = ($Sig->parse("$usernam,", ",", $info));
$gender = ($Sig->parse("$age,", ",", $info));
$friends = ($Sig->parse("<span class=\"redbtext\">", "<", $info));
//Displays	
print "<b>Username:</b> $username<br>";
print "<b>Age:</b> $age<br>";
print "<b>Gender:</b> $gender<br>";
print "<b>Friends:</b> $friends<br>";

?>

 

Just if I could remeber how to do a string and make an array out of this "<meta name="description" content="MySpace Profile - Mark Ryan, 20 years old, Male, CITY, STATE, US,"

Link to comment
Share on other sites

str_replace($CONSTANT, 'replace text', $string);

 

Is what you want for that.

 

So if you combine mine and skali's post then all you need to do is set up a assosiative array of the $CONSTANTS you want to change and maybe edit the str_replace() in the first post if your feeling brave.

 

Don't worry about being a newb either, u gotta start somewhere.  I'm no programmer, by a long shot and I've been playing with php 4 6mnts now and am feeling loads more confident now, keep it up ;)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.