maybe try with php DOMDocument rather than regex. I don’t think all DOMDocument methods will work with just an html fragment and not a complete document, but for your needs it may be enough:
<?php
$dom= new DOMDocument();
$html="<p>Some html with a <a href="http://foo.com/">link</a>. Some more text and <a href="http://bar.com/">another</a> link.</p>";
$dom->loadHTML( $html );
$dom->preserveWhiteSpace = false;
$elements = $dom->getElementsByTagName( 'a' );
foreach ( $elements as $element ){
echo "link text: " . $element->nodeValue . "<br>";
echo "href value: " . $element->getAttribute( 'href' ) . "<br><br>";
}
// outputs:
//
// link text: link
// href value: http://foo.com/
//
// link text: another
// href value: http://bar.com/
Related Posts:
- Changes in permalink structure in WordPress and how to apply to in-site links
- How to get post permalink url without a href
- get_template_directory_uri() providing wrong path for img
- What is the proper way to call a function (from functions.php) on a link click?
- Disable Attachment Pages Completely
- Add a class to links in the visual editor (how to get old dialog back)
- Facebook Comment Count
- How to Remove Parents Category Permalink from Posts
- add_rewrite_rule with bottom priority doesn’t handle the WordPress pages
- Filter link to existing content suggestion
- How is WordPress manipulating the posts like there are folders?
- Featured image fallback link to permalink
- Shortcode return function with link href inside PHP
- How to properly insert a link to a template in WordPress?
- Do changes to WordPress permalink custom structure affect old urls?
- WordPress custom slug (endpoint) and compare all links
- Taxonomy linked to pages
- 200 Rewrite blog post links
- wp_trim_words() does not work with my code Am I doing any mistake in my code?
- WordPress Persistent connection rule does not work for “posts”
- WordPress every page is 404 not found including admin resources
- WordPress returning 404 for multisite pages
- How to use $_GET function WordPress backend
- Create custom permalinks to show Custom Post Type’s relationship?
- Make Current/Active Page Number a Link (WP_LINK_PAGES)
- Trying to prepend a Hashtag symbol to the_tags links [closed]
- Next post link on a bootstrap button
- stripping tags from excerpt in WordPress is not working
- trouble with page_id & highlighting current page
- How to use the wpsnonce clone post link?
- Extract links from content
- Custom post type permalinks do not appear using the link functions
- How to Handle Going Backwards in Navigation When Referrer in PHP/JavaScript won’t work?
- Automatically Add Page Links to Nav Menu
- Search only working on front page (index) , not working on other pages
- with PHP within splits the link
- how to edit a specific post slug using a php code?
- WordPress theme showing blank page
- Adding custom url to readmore link using get_permalink()
- Strip hashtag off permalink with php
- Permalinks: site.com/post-tite vs site.com/post-id/post-title for SEO and Speed
- Can not call .php files after switching to %postname%
- WordPress permalink issue
- How to set all External Domain Links with nofollow Attribute from Header Footer Content and Excerpt using PHP?
- Change permalink structure specific category
- Obfuscate links (for SEO purpose) [closed]
- Custom URL for multiple categories hierarchy
- Adding wp users ID into a link address from a DIVI modul
- WordPress redirects to localhost
- Allow two posts (from different categories) to have the same slug
- Test the existence of a page/article by its URL, taking in consideration the URL rewriting by filters
- Custom PHP form needs refresh to load page correctly
- Child-Theme Category View with modified permalinks (%category% removed)
- How to enable HTML tags in category description without breaking the category page
- Rewrite rule not working, but only when parameter is text
- opens like sitename.com/URL
- Link to index.php from home.php?
- How does Permalink work with the AMP plugin after Removing Parent Permalink Catalog from Posts
- Get WordPress username to customize url
- Permalink doesn’t navigate to post (changes only url)
- Post permalink buttons all going to the first post
- Permalink changes after several minutes after saving post
- WordPress/NGINX not respecting the category URL for new posts
- WordPress generates weird permalinks
- Display child-page links in sidebar on both Parent Pages AND Child Pages
- Permalink like example.com/taxonomy/post_type/postname
- How do I stop my form from adding code to current page URL instead of re-directing. Been stuck for days
- image on homepage disappeared due to change of permalink name
- Custom content using in_category
- Change username link in admin bar
- How do I generate formatted permalinks as specified in backend options?
- 403 Forbidden Localhost Wamp Apache Php
- Passing parameter from the current URL to redirect to another url
- Wrapping shortcode content in a span or link
- how to remove replicating a tag inside loop?
- redirect 301 with special character like WIX site “#!”
- How to solve 404 permalink errors on nginx server
- Some links broken after updating WordPress version
- Links in footer section
- add_rewrite_rule questions
- How to change this internal image link parameter using PHP?
- Weird slug in archives permalink
- Newly published content gives 404 on wordpress
- set new link as permalink
- Multiple URL from Custom post name
- echo variable containing html and the_permalink();
- append links with unique number string
- get_permalink returning first letter
- custom data in url
- Link to external page has wordpress blog-url inside
- Remove /category/ from category (archive) page URLs (without using a plugin)
- Accessing WordPress Functions get_permalink() in Vanilla PHP?
- PHP Button Custom link [closed]
- WordPress add parameters with friendly url structure
- Add any username as link prefix and show data from that specific user’s profile on wordpress pages – with no login required
- how to goto specific page number including title with permalink
- How to extract URLs from wordpress taxonomies
- Remove content links (internal and external), but exclude post at specific categories
- WordPress 6.1.1 UTF8 Slug Limit Increase
- why css file not link?how to create permalink?