Jump to content

Simple Dome to get url NOT inside an <a href tag


bschultz

Recommended Posts

I posted this....http://forums.phpfreaks.com/topic/297984-multiple-regex-on-the-same-string/

 

If you don't want to read the original post, I'm trying to abandon Wordpress, but still use the functionality of a couple of plugins. 

 

Not seeing any responses tells me that maybe I should be barking up a different tree.  Is it possible to parse a database post and look for a URL that is NOT inside an a href tag?

 

I Googling, I didn't see anything in the simpledom examples about scraping for anything OUTSIDE of a tag.

 

Thanks!

 

Link to comment
Share on other sites

I used Wordpress.  I want to bail on Wordpress.  I've moved the Wordpress posts to a new database.  I'm displaying the post on the page.  Using Wordpress, I could have a post that read:

 

This is a post.  Here is an mp3 address.  (with the address shown, no a href tages needed)

 

The Wordpress plugin would the the url and display an html5 audio player on the site for that mp3.  Another plugin would take a Youtube link and embed the movie in a player.

 

I want to scrape, or parse, the database content...pull out the mp3 url's...and replace them with an html5 player.

Link to comment
Share on other sites

Something like this.

<?php
$string = <<<DATA
<div itemprop="commentText" class='post entry-content '>
					<p>I posted this....<a href='http://forums.phpfreaks.com/topic/297984-multiple-regex-on-the-same-string/' class='bbc_url' title=''>http://forums.phpfre...he-same-string/</a></p>
<p> </p>
<p>If you don't want to read the original post, I'm trying to abandon Wordpress, but still use the functionality of a couple of plugins. </p>
<p> </p>
<p>Not seeing any responses tells me that maybe I should be barking up a different tree.  Is it possible to parse a database post and look for a URL that is NOT inside an a href tag?</p>
<p> </p>
<p>I Googling, I didn't see anything in the simpledom examples about scraping for anything OUTSIDE of a tag.</p>
<p> </p>
<p>Thanks!</p>
<p> </p>

<p> I'll post some mp3 links in this string like http://www.stephaniequinn.com/Music/Allegro%20from%20Duet%20in%20C%20Major.mp3	and also try it without the protocol </p> www.stephaniequinn.com/Music/Allegro%20from%20Duet%20in%20C%20Major.mp3 just for good measure.<br />http://www.stephaniequinn.com/Music/Mozart%20-%20Presto.mp3           stephaniequinn.com/Music/Mozart%20-%20Presto.mp3 is not looking for any url patterns.<br />		
					http://www.stephaniequinn.com/Music/Commercial%20DEMO%20-%2004.mp3
					
<p>An acdc sample using ogg:https://upload.wikimedia.org/wikipedia/en/4/45/ACDC_-_Back_In_Black-sample.ogg</p>
<p>m4a sample:http://download.wavetlan.com/SVV/Media/HTTP/AAC/MediaCoder/MediaCoder_test1_HE-AAC_v4_Stereo_VBR_64kbps_44100Hz.m4a</p>
<p>wav sample:http://freewavesamples.com/files/Yamaha-TG500-AP-Dance-C5.wav </p>
		
				</div>
DATA;


function checkProtocol($url){
	if(!parse_url($url, PHP_URL_SCHEME)){
			return "http://".$url;
		}else{
		    return $url;
		}
}

$matches = array();//define array
$string = preg_replace('/\s+/', ' ',$string);//clean multiple whitespace
$pattern = '/((http|https|ftp|ftps)\:\/\/)?[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?\.(mp3|m4a|ogg|wav)/';//match url and audio patterns

$mp3player = preg_replace_callback($pattern, 'callback', $string);//find and replace

//replace callback function
function callback($matches){
	if(!empty($matches[0])){//check if matches exist
    //print_r($matches[0]);
	$mp3_url = checkProtocol($matches[0]);//add http protocol if any missing
	
    return " 
<audio controls>
  <source src='$mp3_url'>
Your browser does not support html5.
</audio>
 ";
}
}

echo $mp3player;//display including players
?>
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.