How to Find YouTube Video ID from Any YouTube URL

Learn how to extract the YouTube video ID from different types of YouTube URLs using simple methods and regex examples. Ideal for developers and marketers.

YouTube has so many types of URLs to embed the video on your site. Sometimes it’s difficult to find a single regular expression to parse all type of YouTube URL and retrieve the video ID from it.

To retrieve the video ID from the YouTube URL, use this function,

function getVideoID($url) {
    $pattern = '#^(?:https?://)?(?:www\.)?(?:youtu\.be/|youtube\.com(?:/embed/|/v/|/watch\?v=|/watch\?.+&v=))([\w-]{11})(?:.+)?$#x';
    preg_match($pattern, $url, $matches);
    return (isset($matches[1])) ? $matches[1] : false;
}

Regular Expression explanation is as follows,

$pattern = '#^(?:https?://)?';    # Either http or https.
$pattern .= '(?:www\.)?';         #  Optional, www subdomain.
$pattern .= '(?:';                #  Group host alternatives:
$pattern .=   'youtu\.be/';       #    Either youtu.be,
$pattern .=   '|youtube\.com';    #    or youtube.com
$pattern .=   '(?:';              #    Group path alternatives:
$pattern .=     '/embed/';        #      Either /embed/,
$pattern .=     '|/v/';           #      or /v/,
$pattern .=     '|/watch\?v=';    #      or /watch?v=,    
$pattern .=     '|/watch\?.+&v='; #      or /watch?other_param&v=
$pattern .=   ')';                #    End path alternatives.
$pattern .= ')';                  #  End host alternatives.
$pattern .= '([\w-]{11})';        # Youtube video ids with standard length of 11 chars.
$pattern .= '(?:.+)?$#x';         # Optional other ending URL parameters.

How to Decode HTML Entities Using jQuery?

Learn how to decode HTML entities in jQuery using smart and efficient methods. Includes working examples and cross-browser techniques.

When working with dynamic HTML content, you might encounter special characters represented as HTML entities (e.g., &lt; for <, &gt; for >). These are essential for rendering text safely in browsers, but sometimes you may need to convert them back to their original characters — a process known as decoding.

In this article, we’ll explore how to decode HTML entities using jQuery in a simple and effective way.

Why Decode HTML Entities?

Most of the browsers convert certain characters into HTML entities to avoid rendering issues or XSS attacks. For example:

  • < becomes &lt;
  • > becomes &gt;
  • & becomes &amp;
  • " becomes &quot;

If you’re extracting or manipulating HTML content through JavaScript or AJAX, you may receive entity-encoded strings. To properly display or process them, you’ll need to decode these entities to a readable format.

jQuery Method to Decode HTML Entities

jQuery itself doesn’t provide a direct function to decode HTML entities, but you can use the browser’s native HTML parsing capability by leveraging a temporary DOM element like textarea.

Example Code:

function decodeHtmlEntities(encodedStr) {
    return $('<textarea/>').html(encodedStr).text();
}

// Usage
var encoded = '&lt;p&gt;Hello World!&lt;/p&gt;';
var decoded = decodeHtmlEntities(encoded);

console.log(decoded); // Output: <p>Hello World!</p>

Explanation:

  • $('<textarea/>') creates a temporary <textarea> element.
  • .html(encodedStr) sets the HTML content with entities.
  • .text() extracts the decoded plain text.

This approach is safe and works across all modern browsers.

Alternative Vanilla JavaScript Method

If you prefer using plain JavaScript without jQuery, here’s an equivalent solution:

function decodeHtmlEntities(encodedStr) {
    var txt = document.createElement('textarea');
    txt.innerHTML = encodedStr;
    return txt.value;
}

// Usage
var decoded = decodeHtmlEntities('&amp;copy; 2025');
console.log(decoded); // Output: © 2025

This is same example in Vanilla Javascript.

Use Case Examples

Below are some examples to demostrate the usage of decodeHtmlEntities function.

Example 1: Decoding API Response

If your API is giving response in HTML entities, you can use this function to decode it.

$.get('/get-description', function(data) {
    var decoded = decodeHtmlEntities(data.description);
    $('#description').text(decoded);
});

Example 2: Decoding User Input Stored with Entities

Sometimes, we store user input data in the database with HTML encoded string. We can use this function to decode HTML entities.

var userInput = '&lt;script&gt;alert(&quot;Hi&quot;)&lt;/script&gt;';
$('#output').text(decodeHtmlEntities(userInput));

Conclusion

Decoding HTML entities is essential when you’re dealing with encoded data from the server or other sources. Using jQuery’s ability to manipulate DOM elements, you can easily decode entities without additional libraries.

This simple technique ensures that your content is displayed correctly, improving both functionality and user experience.

SPF Record for Outgoing Mails: What It Is and How to Set It Up

Learn what an SPF record is, why it’s essential for email deliverability, and how to configure it properly for sending outgoing emails from your domain.

SPF record is used to accurately calculate the reputations of envelope-sender domains and mailservers by mailbox-providers. It works as follows: If many users flag emails as spam from example.com, then SPF allows the mailbox-provider to more accurately say that, “example.com is a spammer”, because the SPF record of example.com is used to identify it as a spam. Conversely, if any user flag spoofed email as spam from example.com, then SPF allows the mailbox-providers to maintain the good reputation for example.com and makes the email flow normal. So, we should always use SPF to make spam filtering more accurate.

As a PHP developer, everyone familiars with the PHP mail (http://php.net/manual/en/function.mail.php) function, which is used for sending emails. But, one can’t send email using SMTP authentication using this mail function. This makes most outgoing emails caught by spam-filters because of the SPF (Sender Policy Framework) check.

An important thing about SPF is that, it only performs checks on the envelope sender (Return-Path header), not on the user-visible from header. You can read about this at http://www.openspf.org/FAQ/Envelope_from_scope.

In most of the application, we use PHP’s mail function. If we check the outgoing mail header sent using the mail function, we can see that the envelope sender (Return-Path header) is a common address of the server or localhost even if you provide the From header of your domain to the mail.

So, when SPF checks performed by mailserver, it checks only the envelope sender which is a common address of the server or localhost and ignoring the from address, which is a genuine email address of the domain. So, our SPF record will be ignored. And, the server may not have an SPF record. In such case mail server detect a softfail. If you notice in Gmail, it identifies such softfail with phrase “best guess record”. Gmail uses a heuristic system to create best-guess SPF records for domains that don’t use SPF. For more details, go to http://www.ceas.cc/2006/19.pdf

As Gmail is using a best-guess records for server/domains that don’t have an SPF record, this guess is sometimes wrong, which cause a softfail. If your mail is being routed through a new mailserver, which may not add to the best-guess SPF records by Gmail, then we can see the softfail disappear after a while.

There is some basic solution to avoid softfails due to SPF record.

One solution would be to use SMTP mailer instead of PHP’s simple mail function. There are many SMTP mailer classes available free, like PHPMailer. SMTP mailer is directly talking to the mail server, so it can set the envelope-sender to be same as the From address. You can also specify the envelope-sender while using SMTP mailer.

Alternatively, we can also use Google’s SMTP (smtp.google.com) to send outgoing mails using Google’s username and password. In this case, we have to simplify our SPF record to identify Google by adding “include:_spf.google.com ~all”.

But, if we have already used a PHP’s mail function throughout the application, and switching to SMTP mailer requires so much time and effort, we can still set an envelope-sender using PHP’s mail function. To set the envelope-sender, use sendmail’s -f/-F command as a fifth additional-options parameter of the PHP’s mail function.

PHP Code:

mail("user@example.com",  "test subject",  "test message",  $headers,  "-F 'Example  Envelope-sender' -f returnpath@example.com");

We can use any of domain email addresses or from address instead of returnpath@example.com in the above code snippet.

There may be other solutions to achieve this. The key is to set the envelope-sender (Return-Path) to the email address of the domain.