how do i "crop" this string to pull out the value
Moderator: General Moderators
-
scheinarts
- Forum Commoner
- Posts: 52
- Joined: Wed Jul 25, 2007 2:37 am
how do i "crop" this string to pull out the value
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
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
- speedy33417
- Forum Contributor
- Posts: 128
- Joined: Sun Jul 23, 2006 1:14 pm
-
scheinarts
- Forum Commoner
- Posts: 52
- Joined: Wed Jul 25, 2007 2:37 am
- speedy33417
- Forum Contributor
- Posts: 128
- Joined: Sun Jul 23, 2006 1:14 pm
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];
}-
scheinarts
- Forum Commoner
- Posts: 52
- Joined: Wed Jul 25, 2007 2:37 am
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
And for my purposes, I need to pass to the video player the t value alone.
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));
}
?>- speedy33417
- Forum Contributor
- Posts: 128
- Joined: Sun Jul 23, 2006 1:14 pm
Code: Select all
<?php
$var = preg_match("/t=(.*?)&rel/", $data, $result) ? $result[1] : "";
?>-
scheinarts
- Forum Commoner
- Posts: 52
- Joined: Wed Jul 25, 2007 2:37 am
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).scheinarts wrote:this winded up doing the job perfect
Code: Select all
$tAlone = substr($tval, 0, strpos($tval, "&")); echo $tAlone;
Just my 2 cents.
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
Just want to correct you here Most often regex IS NOT faster than native string manipulation functions.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).
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).Jcart wrote:Just want to correct you here Most often regex IS NOT faster than native string manipulation functions.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).
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.
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.
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
Code: Select all
function parse_query($q){
$t = explode('&',$q);
foreach($t as $tp){
list($k, $v) = explode('=',$tp);
$a[$k] = $v;
}
return $a;
}