Forum Moderators: coopster

Message Too Old, No Replies

replace text in file - extract

         

jackvull

3:48 pm on Apr 21, 2010 (gmt 0)

10+ Year Member



In a curl function I save an HTML file to a variable called $page.
I need to open $page and find the following occurrence:
s1.addParam("flashvars","file=/admin/files/Blah blah Blah 04.20.2010.flv&image=mediaplayer/preview.jpg&&stretching=uniform");

Any ideas on how to go about that?
I can just replace the whole $page var while the file is open but am having trouble doing that at present.

sned

4:26 pm on Apr 21, 2010 (gmt 0)

10+ Year Member



Are you looking for just the position within the file? If so the strpos [us2.php.net] function should do it.

If you actually want to replace text within the page itself, there is the substr_replace [us2.php.net] function.

jackvull

6:32 pm on Apr 21, 2010 (gmt 0)

10+ Year Member



It's to extract a link and then email it to myself.
So, the curl logs in, extracts an html page but then I need to look through the text find any link ending in .flv and then work back 20 or so characters to the start of the link and email it...
The $page variable contains all the html


<?php

//echo date("G");
if (date("G") >= 21 || date("G") < 13) {exit();}
if (date("N") == 6 || date("N") == 7) {exit();}

$fh = fopen("/usr/local/sbin/myscripts/aaaaa.html", 'w') or die("can't open file fh");

//INIT CURL
$ch = curl_init();

// SET URL FOR THE POST FORM LOGIN
curl_setopt($ch, CURLOPT_URL,
'https://www.aaaaa.com/login.php');

// ENABLE HTTP POST
curl_setopt ($ch, CURLOPT_POST, 1);

// SET POST PARAMETERS : FORM VALUES FOR EACH FIELD
curl_setopt ($ch, CURLOPT_POSTFIELDS,
'_username=aaaaa&password=aaaaa');

// IMITATE CLASSIC BROWSER'S BEHAVIOUR : HANDLE COOKIES
curl_setopt ($ch, CURLOPT_COOKIEJAR, '/usr/local/sbin/myscripts/cookie.txt');

# Setting CURLOPT_RETURNTRANSFER variable to 1 will force cURL
# not to print out the results of its query.
# Instead, it will return the results as a string return value
# from curl_exec() instead of the usual true/false.
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);

// EXECUTE 1st REQUEST (FORM LOGIN)
$store = curl_exec ($ch);

// SET FILE TO DOWNLOAD
curl_setopt($ch, CURLOPT_URL,
'https://www.aaaaa.com/aaaaa.php');
// EXECUTE 2nd REQUEST (FILE DOWNLOAD)
$content = curl_exec ($ch);
fwrite($fh, $content);
fclose($fh);

// CLOSE CURL
curl_close ($ch);

$to = 'aaaaa@aaaaa.net';
$subject = 'aaaaa';
$random_hash = md5(date('r', time()));
$headers = "From: webmaster@aaaaa.co.uk\r\nReply-To: webmaster@aaaaa.co.uk";
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
ob_start(); //Turn on output buffering
?>
--PHP-alt-<?php echo $random_hash; ?>
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
<?
$page = file_get_contents('/usr/local/sbin/myscripts/aaaaa.html');

$regex = '(href|src)\s*=\s*"*\'*\/*([^"\'\s\>]+)(.*?)\s*\/*\s*\>';
$page=preg_replace("/$regex/i","$1=\"http://www.aaaaa.com/$2\$3>",$page);
echo $page;
?>
--PHP-alt-<?php echo $random_hash; ?>--
<?
$message = ob_get_clean();
$mail_sent = @mail( $to, $subject, $message, $headers );

?>

jackvull

6:29 pm on Apr 22, 2010 (gmt 0)

10+ Year Member



any ideas?

jackvull

9:12 am on Apr 26, 2010 (gmt 0)

10+ Year Member



I need to open $page and find the following occurrence:
s1.addParam("flashvars","file=/admin/files/Blah blah Blah 04.20.2010.flv&image=mediaplayer/preview.jpg&&stretching=uniform");

then echo only that link in an email

TheMadScientist

10:57 am on Apr 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It can be done, but it could be a complicated regular expression you're asking for. It depends on the consistency of the match and I don't know what stays the same and what changes from one file to the next, so I don't know exactly what to tell you. If s1.addParam(" will always be the beginning of the string, then it's fairly easy, but if you're looking for only URLs ending in .flv randomly, then to do it efficiently is quite a bit tougher.

You might want to have a look a preg_match (just search for php preg_match() using your favorite SE) and have a try... I'd rather edit and assist than write it for you personally, even if I had all the information I need, because then you'll know how to do it. Someone else might come along and give you a better answer, but it's late where I am and this is the best I've got for you right now.