hello I have the following tags :
/* Youtube Live Embed
* Copyright (C) 2015 Jerod Lycett
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
window.onload = embed;
function embed() {
var apikey = "SANITIZED"; //Set to your API Key
var channelID = "UC_aSKWQBRMoN2wry2Mh3jvg"; //Set to your channel ID. If you don't know it visit https://www.googleapis.com/youtube/v3/channels?part=id&forUsername=CHANNELNAME&fields=items%2Fid&key=APIKEY with your name and key.
var video;
checkLive(channelID,apikey,function() {
video = this;
if(video.length == 0) {
archiveVideo(channelID,apikey,function() {
video = this;
var embedCode = "<object height=\"350\" width=\"425\"><param name=\"movie\" value=\"https://www.youtube.com/v/" + video + "&autoplay=0\"><embed height=\"350\" width=\"425\" type=\"application/x-shockwave-flash\" src=\"https://www.youtube.com/v/" + video + "&autoplay=0\"></embed></object>";
document.getElementById("liveembed").innerHTML = embedCode;
});
}
else {
var embedCode = "<object height=\"350\" width=\"425\"><param name=\"movie\" value=\"https://www.youtube.com/v/" + video + "&autoplay=1\"><embed height=\"350\" width=\"425\" type=\"application/x-shockwave-flash\" src=\"https://www.youtube.com/v/" + video + "&autoplay=1\"></embed></object>";
document.getElementById("liveembed").innerHTML = embedCode;
}
});
}
function checkLive(channelID,apikey,callback) {
// In future check if soon live and refresh until live
var getLive = new XMLHttpRequest();
getLive.onreadystatechange = function() {
if(getLive.readyState === 4) {
var liveVideo = JSON.parse(getLive.responseText);
if(liveVideo.items[0] === undefined) {
callback.call("");
}
else {
callback.call(liveVideo.items[0].id.videoId);
}
}
}
getLive.open("GET","https://www.googleapis.com/youtube/v3/search?part=id&channelId=" + channelID + "&eventType=live&maxResults=1&type=video&fields=items%2Fid&key=" + apikey,true);
getLive.send();
}
function archiveVideo(channelID,apikey,callback) {
var getPlaylist = new XMLHttpRequest();
getPlaylist.onreadystatechange = function() {
if(getPlaylist.readyState === 4) {
var playlistObj = JSON.parse(getPlaylist.responseText);
var playlistID = playlistObj.items[0].contentDetails.relatedPlaylists.uploads;
var getArchiveVideo = new XMLHttpRequest();
getArchiveVideo.onreadystatechange = function() {
if(getArchiveVideo.readyState === 4) {
var archiveVideoObj = JSON.parse(getArchiveVideo.responseText);
callback.call(archiveVideoObj.items[0].contentDetails.videoId);
}
}
getArchiveVideo.open("GET","https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&maxResults=1&playlistId=" + playlistID + "&fields=items%2FcontentDetails&key=" + apikey,true);
getArchiveVideo.send();
}
}
getPlaylist.open("GET","https://www.googleapis.com/youtube/v3/channels?part=contentDetails&id=" + channelID + "&fields=items%2FcontentDetails&key=" + apikey,true);
getPlaylist.send();
}
and this code :
$content ='<a href="http://website.com/" />
<a href="/link1" />
<a href="https://website.com" />
<a href="link1" />';
I want to use the code above to replace href tags doesn't start with an http or https or with a slash .
thanks in advance.
答案 0 :(得分:0)
Something like this should do it. I'd advise using a parser in the future, How do you parse and process HTML/XML in PHP?, for tasks such as this though. This can become very messy quickly. Here's a link on this regex usage as well, http://www.rexegg.com/regex-best-trick.html.
Regex:
x10
Demo: https://regex101.com/r/cV2xB5/1
PHP Usage:
/href=("|')https?:\/\/(*SKIP)(*FAIL)|href=("|')(.*?)\2/
Output:
$content ='<a href="http://website.com/" />
<a href="/link1" />
<a href="https://website.com" />
<a href="link1" />';
echo preg_replace('/href=("|\')https?:\/\/(*SKIP)(*FAIL)|href=("|\')\/?(.*?)\2/', 'href=$2http://website2.com/$3$2', $content);
Update, for <a href="http://website.com/" />
<a href="http://website2.com/link1" />
<a href="https://website.com" />
<a href="http://website2.com/link1" />
exclusion
Use:
//
Demo: https://regex101.com/r/cV2xB5/2
PHP:
href=("|')(?:https?:)?\/\/(*SKIP)(*FAIL)|href=("|')(.*?)\2
Output:
$content ='<a href="//website.com/" />
<a href="/link1" />
<a href="https://website.com" />
<a href="link1" />';
echo preg_replace('/href=("|\')(?:https?:)?\/\/(*SKIP)(*FAIL)|href=("|\')\/?(.*?)\2/', 'href=$2http://website2.com/$3$2', $content);