Solved

Getting portion of php string

Posted on 2011-02-20
15
349 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:Fernanditos
  • 5
  • 4
  • 3
  • +3
15 Comments
 
LVL 16

Expert Comment

by:BurnieP
ID: 34937498
Hi,

Something around this line :

$str = substr($str, 0, strlen(strpos($str,"mp3") + 3))
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34937511
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]);
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 34937522
the parseurl function of php should do it:
http://www.php.net/manual/en/function.parse-url.php
0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 6

Expert Comment

by:MatthewP
ID: 34937618
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:

$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

Open in new window


-output will be:
http://www.site.com/blog/2011/02/01/filetoaudio-baker.mp3
filetoaudio-baker.mp3
0
 

Author Comment

by:Fernanditos
ID: 34937627
@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 ) 

Open in new window

0
 

Author Comment

by:Fernanditos
ID: 34937632
@Roads_Roads your solution wont work if file name contains a whitespace.

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34937637
How about BurineP's solution ?
0
 

Author Comment

by:Fernanditos
ID: 34937649
@MatthewP: your solution wont work if file name contains a whitespace.
0
 

Author Comment

by:Fernanditos
ID: 34937657
Burine's solution does not works. It returns just 2 letters: ht
0
 
LVL 6

Accepted Solution

by:
MatthewP earned 500 total points
ID: 34937680
A simple change then. Is your data always in this format:

 path/to/filename string_of_numbers mime type

if so this will do it:
$str="http://www.site.com/blog/2011/02/01/file toaudio-baker.mp3 6787576 audio/mpeg";
$split_at_space=explode(" ",$str);
$mimetype=array_pop($split_at_space);
$numbers=array_pop($split_at_space);
$file=join(" ",$split_at_space);
echo $file; // print complete url
echo "\n";
echo basename($file); // print file name from url only

Open in new window




0
 
LVL 6

Expert Comment

by:MatthewP
ID: 34937697
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);

Open in new window

0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34937700
<?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";
?>

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34937714
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"
}

<?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);

Open in new window

0
 
LVL 6

Expert Comment

by:MatthewP
ID: 34937767
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:

$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];

Open in new window




0
 

Author Comment

by:Fernanditos
ID: 34937814
@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?

http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_26834559.html

Thank you all!

0

Featured Post

How our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to count occurrences of each item in an array.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question