I am using advanced custom filed wordpress plugin to create a meta tag called youtube URL...
When some one put the video url in shorter format like this https://youtu.be/H-30B0cqh88
Then the iframe I put to show the video doesn't work cause iframe doesn't work with shorter version of url
rather it needs real url as a source.. My iframe code is as below
">
How can I achive this.. Let me show you how I want to achieve this..
I am not that expert on php so please give me the full working code..
<?php
the_field("listing_video_1") == $got_url_from_user_input
if $got_url_from_user_input == https://youtu.be/H-30B0cqh88 in this format
$actual_URL= replace above url to https://youtube.com/embed/H-30B0cqh88
?>
How can I achieve this please.
Thanks in advance
Here's a simple way to use preg_replace to accomplish what you are trying to do:
<?php
// SET OUR DEFAULT URL
$got_url_from_user_input = 'https://youtu.be/H-30B0cqh88';
print "\nSTARTING URL: ".$got_url_from_user_input;
// DO THE REPLACE AND PRINT OUT THE FINAL RESULT
$actual_URL = preg_replace('~https://youtu\.be/([-A-Z0-9]+)~i', 'https://youtube.com/embed/$1', $got_url_from_user_input);
print "\nFINAL URL: ".$actual_URL;
There's not much magic here, so let me run down it quickly:
https://youtu\.be/ - Look for this pattern exactly. We escape the dot with a backslash so it finds a literal dot and not any character.
([-A-Z0-9]+) - This is just your basic character class matching any dash, letter or number, occurring at least one time. We put it in parenthesis so that we it will be saved in $1 and we can plug it into our final url.
Here is the above code in a working demo you can take a look at:
http://ideone.com/zECA2e
You can just use php str_replace() function as in following code :
<?php
$actual_url = str_replace('https://youtu.be/', 'https://www.youtube.com/embed/', $got_url_from_user_input);
echo $actual_url;
?>
str_replace() will replace youtubes short url with embade url.
Related
I'm trying to parse a HTML to retrieve the value of tag, on my Google Apps Script code. contains line breaks in attributes, and appears more than once but I only want the first value. (In this case, only 'foo' is required.)
<b class="
"
>
foo
</b><b class="
"
>
var
</b>
On Google Apps Script, functions such as 'getElementByTagName' is not available. So I first though of using regexp but it's not the wise option here.
Does anyone have an idea on how I can move forward? Any comment/guess would be highly appreciated!
How about using XmlService for your situation as a workaround? At XmlService, even if there are several line breaks in the tags, the value can be retrieved. I think that there are several workarounds for your situation. So please think of this as one of them.
The flow of sample script is as follows.
Flow :
Add the header of xml and a root element tag to the html.
Parse the creates xml value using XmlService.
Retrieve the first value of tags using XmlService.
Sample script :
var html = '<b class="\n"\n>\nfoo\n</b><b class="\n"\n>\nvar\n</b>\n'; // Your sample value
var xml = '<?xml version="1.0"?><sampleContents>' + html + '</sampleContents>';
var res = XmlService.parse(xml).getRootElement().getChildren()[0].getText().trim();
Logger.log(res) // foo
Note :
In this sample script, your sample html was used. So if you use more complicated one, can you provide it? I would like to modify the script.
Reference :
XML Service
If this was not what you want, please tell me. I would like to modify it.
Edit 1 :
Unfortunately, for the value retrieved from the URL, above script cannot be used. So I used "Parser" which is a GAS library for your situation. The sample script is as follows.
Sample script :
var url = "https://www.booking.com/searchresults.ja.html?ss=kyoto&checkin_year=2018&checkin_month=10&checkin_monthday=1&checkout_year=2018&checkout_month=10&checkout_monthday=2&no_rooms=1&group_adults=1&group_children=0";
var html = UrlFetchApp.fetch(url).getContentText();
var res = Parser.data(html).from("<b class=\"\n\"\n>").to("</b>").build().trim();
Logger.log(res) // US$11
Note :
Before you run this script, please install "Parser". About the install of library, you can see it at here.
The project key of the library is M1lugvAXKKtUxn_vdAG9JZleS6DrsjUUV
References :
Parser
Managing libraries
google app script Exceeded memory limit
google script scrape parser with 2 classes with the same name
Edit 2 :
For your 2nd URL in your comment, it seems that the URL is different from your 1st one. And also your new URL has no tag of <b class=\"\n\"\n>. By this, the value you want cannot be retrieved. But from the 1st URL in your comment, I presumed about the value what you want. Please confirm the following script?
var url = "https://www.booking.com/searchresults.ja.html?ss=kyotogranvia&checkin_year=2018&checkin_month=10&checkin_monthday=1&checkout_year=2018&checkout_month=10&checkout_monthday=2&no_rooms=1&group_adults=1&group_children=0";
var html = UrlFetchApp.fetch(url).getContentText();
var res = Parser.data(html).from("<span class=\"lp-postcard-avg-price-value\">").to("</span>").build().trim();
Logger.log(res) // US$289
This url:
url = rtmp://xxxxxxxxxxxxxx.cloudfront.net/cfx/st/mp3:audios/lesson/2/cancion?Expires=1386332537&Signature=mysignature__&Key-Pair-Id=my-key-par-id
I would like to add the extension mp3 to all file name.
In this case the file name is cancion
The id of lesson is a dynamic value.
I would like to get this url something like:
url = rtmp://xxxxxxxxxxxxxx.cloudfront.net/cfx/st/mp3:audios/lesson/2/cancion.mp3?Expires=1386332537&Signature=mysignature__&Key-Pair-Id=my-key-par-id
Thanks!
You can parse the URI, edit the path, then return the value
require 'uri/http'
u = URI.parse('rtmp://xxxxxxxxxxxxxx.cloudfront.net/cfx/st/mp3:audios/lesson/2/cancion?Expires=1386332537&Signature=mysignature__&Key-Pair-Id=my-key-par-id')
u.path += ".mp3"
puts u.to_s
or use a simple regexp replace
u = 'rtmp://xxxxxxxxxxxxxx.cloudfront.net/cfx/st/mp3:audios/lesson/2/cancion?Expires=1386332537&Signature=mysignature__&Key-Pair-Id=my-key-par-id'
u.gsub('?', '.mp3?')
The second approach can be used only if you can assume the format of the input is always the same.
You can do simple gsub since this is URL and you can expect one occurrence of ? so simple do.
url.gsub!('?', '.mp3?')
Usually I would go regex here but no need from previously stated reason.
I would like to grab the following value on this website with xquery. After trying for awhile I couldn't figure it out. Here is what I am trying to fetch (image link)
And the code I tried using:
$xpath = new DOMXPath( $html );
$nodelist = $xpath->query( "???????????????" );
Any ideas?
You can try to grep the value between script tags
Like
//form/div/div/div[#class="values span-7"]/skipt/Text()
After parse the value in PHP other language hat you use to get your string
Can you get the value off of the input?
//form//input[#name="val7"]/#value
Update:
You can get the stuff in the script tag, but that's as far as you can get using xpath. You'll then need to parse the contents of the script tag in order to get that value.
//form[#id="werte"]//div[#class="calval7"]//div[#class="values"][1]/script/text()
I'm writing a site with a custom tweet button that uses the www.twitter.com/share function, however the problem I am having is including hash '#' characters within the tweet text.
For example:
http://www.twitter.com/share?url=www.example.com&text=I+am+eating+#branstonpickel+right+now
The tweet text comes out as 'I am eating' and omits the hash and everything after.
I had a quick look on the Twitter forums and learnt the hash '#' character cannot be part of the share url. On https://dev.twitter.com/discussions/512#comment-877 it was said that:
Hashes are special characters in the URL (they identify document fragments) so they, and anything following, does not get sent the server.
and
you need to URLEncode it, so use %23
When I tried the 2nd point in my test link:
www.twitter.com/share?url=www.example.com&text=I+am+eating+%23branstonpickel+right+now
The tweet text came out as 'I am eating %23branstonpickel right now' literally including %23 instead of converting it to a hash.
Sorry for the waffely question, but does anyone know what it is I'm doing wrong?
Any feedback would be greatly appreciated :)
It looks like this is the basic setup:
https://twitter.com/intent/tweet?
url=<url to tweet>
text=<text to tweet>
hashtags=<comma separated list of hashtags, with no # on them>
This would pre-built a tweet of: <text> <url> <hashtags>
The above example would be:
https://twitter.com/intent/tweet?url=http://www.example.com&text=I+am+eating+branston+pickel+right+now&hashtags=bransonpickel,pickles
There used to be a bug with the hashtags parameter... it only showed the first n-1 hashtags. Currently this is fixed.
you can use %23 instead of hash (#) in url eg
http://www.twitter.com/share?url=www.example.com&text=I+am+eating+%23branston+%23pickel+right+now
I may be wrong but i think the hashtag has to be passed as a separate variable that will appear at the end of your tweet ie:
http://www.twitter.com/share?url=www.example.com&text=I+am+eating+branston+pickel+right+now&hashtag=bransonpickel
will result in "I am eating branston pickel right now #branstonpickle"
On a separate note, I think pickel should be pickle!
Cheers
Toby
use encodeURIComponent to encode the url
If you're using PHP, you can use the following:
<?php echo 'http://www.twitter.com/share?' . http_build_query(array(
'url' => 'http://www.example.com',
'text' => 'I am eating #branstonpickel right now'
)); ?>
This will do all the URL encoding for you, and it's easy to read.
For more information on the http_build_query, see the PHP manual:
http://us2.php.net/http_build_query
For url with line jump, # , # and special unicode in it, the following works :
var lineJump = encodeURI(String.fromCharCode(10)),
hash = "%23", arobase="%40",
tweetText = 'https://twitter.com/intent/tweet?text=Le signe chinois '+hans+' '+item.pinyin+': '+item.definition.replace(";",",")+'.'
+lineJump+'Merci '+arobase+'Inalco_Officiel '+arobase+'CRIparis ❤️🇨🇳 '
+lineJump+hash+'Chinois '+hash+'MOOC'
+lineJump+'https://hanzi.cri-paris.org/',
tweetTxtUrlEncoded = tweetText+ "" +encodeURIComponent('#'+lesson+encodeURIComponent(hans));
urlencode
https://twitter.com/intent/tweet?text=<?= urlencode("I am eating #branstonpickel right now"); ?>"
You can just use this code and modify it
20% means space
23% means hashtag
In JS you can easily encode the special characters using encoreURIComponent.
(Warning: don't use encodeURI as "#" and "#" are not escaped.)
Here's an example with mention and hashtag:
const text = "Hello #world ! Go follow #StackOverflow";
const tweetUrl = `https://twitter.com/intent/tweet?text=${ encodeURIComponent(text) }`;
I am quite newbie on AutoIT, I've done alot of things already.
But I can't find out the way to read specific information from the website that is between div or something. If thats not possible, maybe there is a way to read a raw *.txt(or php) file from the website?
You can use the following code example for your URL:
#include <IE.au3>
$oIE = _IECreate("http://test.com/check.php")
$divs = _IETagNameGetCollection($oIE, "div")
For $div In $divs
If $div.className == "content-container" Then
MsgBox(0, "Bestimmter Inhalt", $div.innerText)
EndIf
Next
_IEQuit($oIE)
You asked for the "raw Text", which is best read with the attribute innerText. See MSDN for more details.
This example basically opens a new IE window with the given URL. Then it extracts all the div-Elements and iterates through them in a loop. Once the className is "content-container" the content of this element is displayed in a message box. Finally the IE window is closed again.
You could add the parameters , 0, 0 to the _IECreate() function to hide the browser window in the end.
Instead of searching for a specific <div>-Element as shown here you could just use the following two lines...
$body = _IETagNameGetCollection($oIE, "body", 0)
MsgBox(0, "Komplette Webseite", $body.innerText)