1

See i have an url in a html code

<a href="http://b48.ve.vc/b/data/48/3746/05 Dabangg Reloaded_-_www.DjPunjab.Com.mp3" rel="nofollow" target="_blank" style="color:green;">play</a>       

Now i want to print this url as it is written in a php page

http://b48.ve.vc/b/data/48/3746/05 Dabangg Reloaded_-_www.DjPunjab.Com.mp3

You can see that between the url 05 Dabangg Reloaded their is space. I made this program to print url from this html code..

$str = "<a href="http://b48.ve.vc/b/data/48/3746/05 Dabangg Reloaded_-_www.DjPunjab.Com.mp3" rel="nofollow" target="_blank" style="color:green;">play</a>";

$pattern = '`.*?((http|ftp)://[\w#$&+,\/:;[email protected]]+)[^\w#$&+,\/:;[email protected]]*?`i';
if (preg_match_all($pattern,$str,$matches))


foreach($matches[1] as $data)
{
$str=$data;
echo $str; 
}

Then i am getting this

http://b48.ve.vc/b/data/48/3746/05 

please do not mention on foreach($matches[1] as $data) line bcoz i am using it with so many urls.. I just want to know how to print the whole url in this format.

http://b48.ve.vc/b/data/48/3746/05 Dabangg Reloaded_-_www.DjPunjab.Com.mp3

Spaces are become a huge matter.. Do not know how to fix it.. What i need to add inside

$pattern = '`.*?((http|ftp)://[\w#$&+,\/:;[email protected]]+)[^\w#$&+,\/:;[email protected]]*?`i';

For making it completely workable. Please suggest me any idea.

2
  • Filenames in URL need prepare using urlencode() Commented May 3, 2013 at 12:53
  • It's illegal for URLs to have spaces in them. Encode the spaces as %20. Commented May 3, 2013 at 13:26

2 Answers 2

2
$str = '<a href="http://b48.ve.vc/b/data/48/3746/05 Dabangg Reloaded_-_www.DjPunjab.Com.mp3" rel="nofollow" target="_blank" style="color:green;">play</a>';
$arr = explode("\"", $str);
$pattern = '`.*?((http|ftp)://[\w#$&+,\/:;[email protected]]+)[^\w#$&+,\/:;[email protected]]*?`i';
$url = preg_grep($pattern,$arr);
$url = implode('',$url);
Output: $url = 'http://b48.ve.vc/b/data/48/3746/05 Dabangg Reloaded_-_www.DjPunjab.Com.mp3'


Update: 2nd Solution [Reference-DOMElement].

$str = '<a href="http://b48.ve.vc/b/data/48/3746/05 Dabangg Reloaded_-_www.DjPunjab.Com.mp3" rel="nofollow" target="_blank" style="color:green;">play</a>';
$DOM = new DOMDocument;
$DOM->loadHTML($str);
$search_item = $DOM->getElementsByTagName('a');
foreach($search_item as $search_item) {
    $url = $search_item->getAttribute('href');
}
echo $url; //Output: http://b48.ve.vc/b/data/48/3746/05 Dabangg Reloaded_-_www.DjPunjab.Com.mp3
Sign up to request clarification or add additional context in comments.

6 Comments

I am using $pattern = '.*?((http|ftp)://[\w#$&+,\/:;[email protected]]+)[^\w#$&+,\/:;[email protected]]*?i'; this code for getting url from html but due to " " spaces i am not getting the complete url..This is my problem.. I think str_replace can not give me complete url from the html code. I need something by which i can get the complete url inside $str; ..
I am telling that you can drink your soup using a spoon. But you are being adamant that you want to drink it with FORK!!! Why to complicate this when there's a predefined function for it...!!!
Man i know that but try to understand that i have url with some HTML code and so i need to remove all the HTML code for that i am using $pattern = ... this code is helping for getting rid of from all the HTML code. But this code is not printing complete url it just stop when it got empty space between the url
@Kapil: U should consider JEES comment before accepting my answer: "The only one thing he needs to take in considration when excuting your or his code is that the href attribute MUST BE the first attribute in the anchor, like if it was <a id="smth" href="http://etc.." it won't work, same thing can be said for his pattern matching"
I also got it solved. I have used a very similar trick like you. Thanks for the direction.
|
1

You can str_replace each one -space- with %20 for encoding your URL

<?php
 $url_org = 'http://b48.ve.vc/b/data/48/3746/05 Dabangg Reloaded_-_www.DjPunjab.Com.mp3';
 $url_edited = str_replace(" ", '%20', $url_org);
?>
 <a href="<?php echo $url_edited; ?>" target="_blank"> HERE </a>

This will work.

4 Comments

I am using $pattern = '.*?((http|ftp)://[\w#$&+,\/:;[email protected]]+)[^\w#$&+,\/:;[email protected]]*?i'; this code for getting url from html but due to " " spaces i am not getting the complete url..This is my problem.. I think str_replace can not give me complete url from the html code. I need something by which i can get the complete url inside $str; ..
Apply the string replace function on the href="http://b48.ve... first and then excute the preg_match, you need to get rid of spaces before the preg.
I did that man but nothing happened. $str = "<a href="b48.ve.vc/b/data/48/3746/05 Dabangg Reloaded_-_www.DjPunjab.Com.mp3" rel="nofollow" target="_blank" style="color:green;">play</a>"; $str1 = $string; str = str_replace(' ', '%20', $str1); $pattern = '.*?((http|ftp)://[\w#$&+,\/:;[email protected]]+)[^\w#$&+,\/:;[email protected]]*?i'; if (preg_match_all($pattern,$str1,$matches)) foreach($matches[1] as $data) { $str1=$data; echo $str1; } } but nothing happened
@elavarasan lee The only one thing he needs to take in considration when excuting your or his code is that the href attribute MUST BE the first attribute in the anchor, like if it was <a id="smth" href="http://etc.." it won't work, same thing can be said for his pattern matching

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.