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
https://forums.phpfreaks.com/topic/39643-get-html-source-into-a-string/
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 ;)

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,"

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 ;)

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.