How can I extract the URL of a link from a post?

I am pretty new in WordPress and I have the following doubt.

I have a page that take some informations from a list of post that belong to a specific category and show these informations into something like a "dashboard".

So I have this code:

?php
/**
 * The template for displaying all Parallax Templates.
 *
 * @package accesspress_parallax
 */
?
    div class="service-listing clearfix"
    ?php 
        $args = array(
            'cat' = $category,
            'posts_per_page' = -1
            );
        $count_service = 0;
        $query = new WP_Query($args);
        if($query-have_posts()):
            $i = 0;
            while ($query-have_posts()): $query-the_post();
            $i = $i + 0.25;
            $count_service++;
            $service_class = ($count_service % 2 == 0) ? "even wow fadeInRight" : "odd wow fadeInLeft";
        ?

        div class="clearfix service-list ?php echo $service_class; ?" data-wow-delay="?php echo $i; ?s"
            div class="service-image"
                ?php if(has_post_thumbnail()) : 
                $image = wp_get_attachment_image_src(get_post_thumbnail_id(get_the_ID()),'thumbnail'); ?
                    img src="?php echo esc_url($image[0]); ?" alt="?php the_title(); ?"
                ?php else: ?
                    img src="?php echo get_template_directory_uri(); ?/images/no-image.jpg" alt="?php the_title(); ?"
                ?php endif; ?
            /div

            div class="service-detail"
                h3?php the_title(); ?/h3
                div class="service-content"?php the_content(); ?/div
            /div
        /div

        ?php 
        if($count_service % 2 == 0): ?
            div class="clearfix"/div
        ?php endif;
        ?

        ?php
            endwhile;
            wp_reset_postdata();
        endif;
    ?
    /div!-- #primary --

As you can see in the previous code, the script retrieve the posts list performing a query and the interate on this list showing this post in a specific way.

So each post content is shown in my page by these lines:

div class="service-detail"
    h3?php the_title(); ?/h3
    div class="service-content"?php the_content(); ?/div
/div

That is rendered into the browser as something like this:

div class="service-detail"
    h3TEST/h3

    div class="service-content"
        pa href="https://it.wikipedia.org/wiki/Pagina_principale"test/a/p
    /div
/div

So, as you can seem into the div having class="service-content" is contained the content value retrieved by the the_content() function.

Now I need to perform the following operation.

If the value retrieved by the the_content() function is an HTML link (the value of the a haref tag) it is putted into a variable (named url, otherwise this variable is setted to null.

Can I do it in some simple way? Exist some WordPress function that retrieve the list of links (in my specific case only one) that are into a post?

If it don't exist how can I check if the post content is an URL and if it is put its value into a variable?

Tnx

Topic the-content urls posts Wordpress

Category Web


Exist some WordPress function that retrieve the list of links (in my specific case only one) that are into a post?

Check out the core wp_extract_urls() function:

$urls = wp_extract_urls( $content );

that will extract urls from any given content.

Example

$content = 'Two test sites: <a href="http://foo.tld">Foo.tld</a> and https://bar.tld';
$urls    = wp_extract_urls( $content );
print_r( $urls  );

with output:

Array
(
    [0] => http://foo.tld
    [1] => https://bar.tld
)

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.