Created
August 2, 2012 22:40
-
-
Save hemache/3241511 to your computer and use it in GitHub Desktop.
linkParser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var patt_link = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; | |
| var link_anchor = '<a href="$1" target="_blank">$1</a>'; | |
| var patt_youtube_match = http://(?:www\.)?youtu(?:be\.com/watch\?v=|\.be/)(\w*)(&(amp;)?[\w\?=]*)?; | |
| var patt_youtube_replace = /https?:\/\/(?:[0-9A-Z-]+\.)?(?:youtu\.be\/|youtube\.com\S*[^\w\-\s])([\w\-]{11})(?=[^\w\-]|$)(?![?=&+%\w]*(?:['"][^<>]*>|<\/a>))[?=&+%\w-]*/ig; | |
| var youtube_iframe = '<iframe width="560" height="315" src="http://www.youtube.com/embed/$1" frameborder="0" allowfullscreen></iframe>'; | |
| function linkText(text) | |
| { | |
| var urls = text.match(patt_link); | |
| for(url in urls) | |
| { | |
| // اذا كان يو ار ال من موقع يوتوب | |
| if( url.match(patt_youtube_match) !== null ) | |
| { | |
| // استبدال اللينك بفرام اليوتوب | |
| text.replace(url, url.replace(patt_youtube_replace, youtube_iframe)); | |
| } | |
| else | |
| { | |
| // استبدال يو ار ال بلينك الهتمل | |
| text.replace(url, url.replace(patt_link, link_anchor)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment