Page 1 of 1

how do i "crop" this string to pull out the value

Posted: Wed Dec 12, 2007 11:10 am
by scheinarts
Hi,

I have this string: t=OEgsToPDskLXWHqoZJpBi8A5bxP1uOs3&rel=1&border=0

And I need to be able to print or echo just the t variable. How how can I achieve that with php code?

Thanks for any suggestions

Posted: Wed Dec 12, 2007 11:11 am
by speedy33417
Is the length of the string you need always the same length?

Posted: Wed Dec 12, 2007 11:20 am
by scheinarts
yes it is... the value of t will always be differente (dynamic) and I just need to isolate it. Hopefully you can guide me how to do it.

Posted: Wed Dec 12, 2007 11:23 am
by speedy33417
It's nothing fancy but you could do this:

Code: Select all

//$length is the length you need from $t
$length = 24;
unset ($croppedString);
for ($i = 0; $i < $length; $i++)
{
	$croppedString .= $t[$i];
}

Posted: Wed Dec 12, 2007 11:30 am
by stereofrog
parse_string()

Posted: Wed Dec 12, 2007 11:32 am
by scheinarts
and what if the value was not always 24... this is a string from you tube... its actually the direct path to the flv file... you can see it here like this

link
thats just a random video... but I need to be able to isolate that t value to "extract" the video. I have no way of knowing if the length of that string will always be 24

So to be exact here is the code the does all this magic of getting that string dynamically just by feeding it the video_id

Code: Select all

<?php

ini_set("max_execution_time","3600");
$baseUrl = "http://www.youtube.com/v/%s";
$idVideo = "3IcwG0jUFxU";
$urlVideo = sprintf($baseUrl,$idVideo);
$baseVideoUrl = "http://www.youtube.com/get_video.php?%s";

ob_start();
// get via CuRL da great library so fine 
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $urlVideo);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_FOLLLOWLOCATION, true);
curl_exec($ch);
curl_close($ch);
$out = ob_get_clean();

$strs = explode("&t=",$out);
$final = explode("\n",$strs[1]);
$t = $final[0];
$finalUrlVideo = getVideoUrl($idVideo,$t);
echo $finalUrlVideo;

$filv = file_get_contents($finalUrlVideo);
$fo = fopen("file.flv","w+");
$fw = fwrite($fo,$filv);
$fc = fclose($fo);

/* getVideoUrl ( string idVideo , string tValue ) */
function getVideoUrl ($id,$tval) {
	global $baseVideoUrl;
	echo "<br>";
	echo "t " . $tval;	
	return sprintf($baseVideoUrl,"video_id=".$id."&t=".trim($tval));
}

?>
And for my purposes, I need to pass to the video player the t value alone.

Posted: Wed Dec 12, 2007 11:43 am
by speedy33417
That's exactly why I asked if the string was always the same length...

Try parse_str() as suggested above.

Posted: Wed Dec 12, 2007 1:56 pm
by aliasxneo

Code: Select all

<?php

$var = preg_match("/t=(.*?)&rel/", $data, $result) ? $result[1] : "";

?>
Wrote that without test but give it a shot.

Posted: Wed Dec 12, 2007 6:21 pm
by scheinarts
this winded up doing the job perfect

Code: Select all

$tAlone = substr($tval, 0, strpos($tval, "&"));
	echo $tAlone;

Posted: Wed Dec 12, 2007 6:28 pm
by aliasxneo
scheinarts wrote:this winded up doing the job perfect

Code: Select all

$tAlone = substr($tval, 0, strpos($tval, "&"));
	echo $tAlone;
I would suggest doing research into RegEx for future jobs like this. For one there versatile, can be re-used over and over again (I have a huge library of expressions), their faster than string manipulation, and they are more likely to work dynamically (meaning there harder to break since you are 'matching' a string).

Just my 2 cents.

Posted: Wed Dec 12, 2007 6:42 pm
by John Cartwright
aliasxneo wrote:I would suggest doing research into RegEx for future jobs like this. For one there versatile, can be re-used over and over again (I have a huge library of expressions), their faster than string manipulation, and they are more likely to work dynamically (meaning there harder to break since you are 'matching' a string).
Just want to correct you here Most often regex IS NOT faster than native string manipulation functions.

Posted: Wed Dec 12, 2007 7:16 pm
by aliasxneo
Jcart wrote:
aliasxneo wrote:I would suggest doing research into RegEx for future jobs like this. For one there versatile, can be re-used over and over again (I have a huge library of expressions), their faster than string manipulation, and they are more likely to work dynamically (meaning there harder to break since you are 'matching' a string).
Just want to correct you here Most often regex IS NOT faster than native string manipulation functions.
I was debating on whether to include that or not, because it truly depends on how the language deals with the RegEx. For instance regex interpretation in Perl is actually proven to be faster than in PHP (partly because of it's native inclusion but also how it interprets and handles it).

But the general rule of thumb is to use RegEx when parsing large amounts of textual data. If I wanted to remove the exclamation mark at the end of a sentence I would obviously use native string manipulation. On the other hand, like in a recent project I did, if I wanted to parse all the movie titles and descriptions off of a YouTube search page I would of course use RegEx.

Posted: Thu Dec 13, 2007 6:36 am
by s.dot
Why not just explode on the =, if the string is always in the same order.

Code: Select all

$t = explode('=', $string);
$t = $t[1];

echo $t;

Posted: Thu Dec 13, 2007 8:34 am
by Kieran Huggins

Code: Select all

function parse_query($q){
	$t = explode('&',$q);
	foreach($t as $tp){
		list($k, $v) = explode('=',$tp);
		$a[$k] = $v;
	}
	return $a;
}
Regex would be more forgiving though, how often are you parsing these strings?