Fernanditos
asked on
Getting portion of php string
Hi,
I have this kind of string:
$str = "http://www.site.com/blog/2011/02/01/filetoaudio-baker.mp3 6787576 audio/mpeg"
How can I get only the link to file portion? it is always a mp3 file.
Thank you.
I have this kind of string:
$str = "http://www.site.com/blog/2011/02/01/filetoaudio-baker.mp3 6787576 audio/mpeg"
How can I get only the link to file portion? it is always a mp3 file.
Thank you.
Or - if this is always an address at the beginning,
$str = "http://www.site.com/blog/2011/02/01/filetoaudio-baker.mp3 6787576 audio/mpeg";
$strarr = explode(" ",$str);
$myfile = trim($strarr[0]);
$str = "http://www.site.com/blog/2011/02/01/filetoaudio-baker.mp3 6787576 audio/mpeg";
$strarr = explode(" ",$str);
$myfile = trim($strarr[0]);
the parseurl function of php should do it:
http://www.php.net/manual/en/function.parse-url.php
http://www.php.net/manual/en/function.parse-url.php
To get just the file name, explode the string at spaces, if you want the file without the path use basename to get only the file portion - this example gives you both:
-output will be:
http://www.site.com/blog/2011/02/01/filetoaudio-baker.mp3
filetoaudio-baker.mp3
$str="http://www.site.com/blog/2011/02/01/filetoaudio-baker.mp3 6787576 audio/mpeg";
@list($file,$extra_data)=explode(" ",$str);
echo $file; // print complete url
echo "\n";
echo basename($file); // print file name from url only
-output will be:
http://www.site.com/blog/2011/02/01/filetoaudio-baker.mp3
filetoaudio-baker.mp3
ASKER
@angelIII: Using that function I get the non desired strins in the path returned:
Array ( [scheme] => http [host] => www.blogtalkradio.com [path] => /newcaptainsofind ustry/2011/02/01/america-west-resources-ceo-dan-baker.mp3 6787576 audio/mpeg )
ASKER
@Roads_Roads your solution wont work if file name contains a whitespace.
How about BurineP's solution ?
ASKER
@MatthewP: your solution wont work if file name contains a whitespace.
ASKER
Burine's solution does not works. It returns just 2 letters: ht
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
That is assuming one space only as a separator between the various parts. If there may be more swap line 2 for:
$split_at_space=preg_split("/\s+/",$str);
<?php
$str = "http://www.site.com/blog/2011/02/01/file toaudio-baker.mp3 6787576 audio/mpeg";
$mystr = trim(substr($str, 0, strpos($str,"mp3")+3));
echo"$mystr";
?>
http://www.laprbass.com/RAY_temp_fernanditos.php
Outputs:
array(3) {
[0]=>
string(57) "http://www.site.com/blog/2011/02/01/filetoaudio-baker.mp3"
[1]=>
string(7) "6787576"
[2]=>
string(10) "audio/mpeg"
}
Outputs:
array(3) {
[0]=>
string(57) "http://www.site.com/blog/2011/02/01/filetoaudio-baker.mp3"
[1]=>
string(7) "6787576"
[2]=>
string(10) "audio/mpeg"
}
<?php // RAY_temp_fernanditos.php
error_reporting(E_ALL);
echo "<pre>";
// TEST DATA FROM THE POST AT EE
$str = "http://www.site.com/blog/2011/02/01/filetoaudio-baker.mp3 6787576 audio/mpeg";
// USE EXPLODE TO ISOLATE THE PARTS OF THE STRING
$arr = explode(' ', $str);
// SHOW THE ISOLATED PARTS
var_dump($arr);
Oh, that said, using that method if you may have multiple spaces together as separators there will be more work if the file name may also contain multiple spaces as part of the name.
Another way, actually with much less code:
Another way, actually with much less code:
$str="http://www.site.com/blog/2011/02/01/file toaudio-baker.mp3 6787576 audio/mpeg";
$result=preg_match("/.*\.\w+/",$str,$matches);
echo $matches[0];
ASKER
@Roads, sorry I did not see your last solution. Matthew's solution is working great.
However if I give a dynamic value to $str instead of static ".." then $file returns empty.
I have opened another question for that related issue. Can you help there?
https://www.experts-exchange.com/questions/26834559/Passing-simple-php-value-in-Wordpress-single-php-file.html
Thank you all!
However if I give a dynamic value to $str instead of static ".." then $file returns empty.
I have opened another question for that related issue. Can you help there?
https://www.experts-exchange.com/questions/26834559/Passing-simple-php-value-in-Wordpress-single-php-file.html
Thank you all!
Something around this line :
$str = substr($str, 0, strlen(strpos($str,"mp3") + 3))