Jump to content

How to filter out certain part of text


DJHandsfree

Recommended Posts

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.. :confused:

Link to comment
Share on other sites

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
)

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.