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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scheinarts
Forum Commoner
Posts: 52
Joined: Wed Jul 25, 2007 2:37 am

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

Post 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
User avatar
speedy33417
Forum Contributor
Posts: 128
Joined: Sun Jul 23, 2006 1:14 pm

Post by speedy33417 »

Is the length of the string you need always the same length?
scheinarts
Forum Commoner
Posts: 52
Joined: Wed Jul 25, 2007 2:37 am

Post 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.
User avatar
speedy33417
Forum Contributor
Posts: 128
Joined: Sun Jul 23, 2006 1:14 pm

Post 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];
}
User avatar
stereofrog
Forum Contributor
Posts: 386
Joined: Mon Dec 04, 2006 6:10 am

Post by stereofrog »

parse_string()
scheinarts
Forum Commoner
Posts: 52
Joined: Wed Jul 25, 2007 2:37 am

Post 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.
User avatar
speedy33417
Forum Contributor
Posts: 128
Joined: Sun Jul 23, 2006 1:14 pm

Post by speedy33417 »

That's exactly why I asked if the string was always the same length...

Try parse_str() as suggested above.
aliasxneo
Forum Contributor
Posts: 136
Joined: Thu Aug 31, 2006 12:01 am

Post by aliasxneo »

Code: Select all

<?php

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

?>
Wrote that without test but give it a shot.
scheinarts
Forum Commoner
Posts: 52
Joined: Wed Jul 25, 2007 2:37 am

Post by scheinarts »

this winded up doing the job perfect

Code: Select all

$tAlone = substr($tval, 0, strpos($tval, "&"));
	echo $tAlone;
aliasxneo
Forum Contributor
Posts: 136
Joined: Thu Aug 31, 2006 12:01 am

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
aliasxneo
Forum Contributor
Posts: 136
Joined: Thu Aug 31, 2006 12:01 am

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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;
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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?
Post Reply