DJHandsfree Posted January 31, 2012 Share Posted January 31, 2012 Hi, I have spend a few day's trying to get my head around how to make this code work as I'm still quite new to PHP The code below so far matches all text from a webpage within the <h3></h3> tags as within them is a url I need. Then that URL within all those h3 tags I only need the text after the last slash of the link so I can put into a flash object and load the requested value. As shown below heres what I have: <html> <head> <title>Find SC Links</title> </head> <body> <?php /* Have a look at the url below within the tags I only need the last part of the url after the / from all the text string <h3><a href="/anyfolder/recording-1-week">link1</a></h3> so just need recording-1-week <h3><a href="/anyfolder/recording-2-week">link2</a></h3> so just need recording-2-week <h3><a href="/anyfolder/recording-3-week">link3</a></h3> so just need recording-3-week then echo them out within this below echo '<div class="bbvideo"><object height="81" width="550"><param name="movie" value="'.$text_string_after_last_slash.'"></param><param name="allowscriptaccess" value="always"></param><embed allowscriptaccess="always" height="81" src="http://player.soundcloud.com/player.swf?url='.$text_string_after_last_slash.'" type="application/x-shockwave-flash" width="550"></embed></object></div><p>'; */ $data = file_get_contents('http://soundcloud.com/dj-handsfree/'); preg_match_all ("/<h3>([^`]*?)<\/h3>/", $data, $matches); $content = $matches[1]; //echo $content; $total = count($content); for($i=0; $i<$total; $i++) { echo $content[$i]."<br />"; } ?> </body> </html> Any help on pointing me in the right direction on this one be much help spend to much of my little time on this one.. Quote Link to comment https://forums.phpfreaks.com/topic/256123-how-to-filter-out-certain-part-of-text/ Share on other sites More sharing options...
Pikachu2000 Posted January 31, 2012 Share Posted January 31, 2012 Are you successfully getting the URLs you need? You can explode on the slash, the use array_pop to get everything after the last slash. If there is a possibility there will ever be a trailing slash in the string, add rtrim to take care of that. Quote Link to comment https://forums.phpfreaks.com/topic/256123-how-to-filter-out-certain-part-of-text/#findComment-1313006 Share on other sites More sharing options...
scootstah Posted January 31, 2012 Share Posted January 31, 2012 How about this? $str = '<h3><a href="/anyfolder/recording-1-week">link1</a></h3> <h3><a href="/anyfolder/recording-2-week">link2</a></h3> <h3><a href="/anyfolder/recording-3-week">link3</a></h3>'; preg_match_all('/<h3><a href="(.*)">.*<\/a><\/h3>/', $str, $matches); $files = array(); foreach($matches[1] as $match) { preg_match('/\/([a-z0-9-]+)$/i', $match, $file_matches); $files[] = $file_matches[1]; } $files will then contain an array of the end of the URL, IE: Array ( [0] => recording-1-week [1] => recording-2-week [2] => recording-3-week ) Quote Link to comment https://forums.phpfreaks.com/topic/256123-how-to-filter-out-certain-part-of-text/#findComment-1313008 Share on other sites More sharing options...
DJHandsfree Posted January 31, 2012 Author Share Posted January 31, 2012 Thanks for your input guys, I have got it doing the job but there's one more thing I need to get accomplished and il be happy! Here's the code below so far <?php /* Test URL http://djhandsfree.co.uk/sclinks.php */ /* Text to match <h3><a href="/dj-handsfree/recording-from-www-flexfm-co">Recording from www.flexfm.co.uk / 99.7FM - Wednesday 10pm-12am 04/01/12</a></h3> */ //Sound Cloud Page $data = file_get_contents('http://soundcloud.com/dj-handsfree/'); //Match required text preg_match_all('/<h3><a href="\/dj-handsfree\/(.*)">.*<\/a><\/h3>/', $data, $matches); $content = $matches[1]; //Output $total = count($content); for($i=0; $i<$total; $i++) { echo '<b>'.$content[$i].'</b><br /><object height="81" width="550"><param name="movie" value="http://soundcloud.com/dj-handsfree/'.$content[$i].'"></param><param name="allowscriptaccess" value="always"></param><embed allowscriptaccess="always" height="81" src="http://player.soundcloud.com/player.swf?url=http://soundcloud.com/dj-handsfree/'.$content[$i].'" type="application/x-shockwave-flash" width="550"></embed></object><p><br />'; } ?> If you see the demo url http://djhandsfree.co.uk/sclinks.php you can see I have used the same matched text as the title of each track but it's not really perfect as the actual title should be reflected by the part underlined <h3><a href="/dj-handsfree/recording-from-www-flexfm-co">Recording from www.flexfm.co.uk / 99.7FM - Wednesday 10pm-12am 04/01/12</a></h3> If anyone could suggest any ideas of where to take it next be great! Quote Link to comment https://forums.phpfreaks.com/topic/256123-how-to-filter-out-certain-part-of-text/#findComment-1313140 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.