PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, November 20, 2022

[FIXED] How can I strip_tags except anchors with remote links?

 November 20, 2022     php, preg-replace, replace, str-replace, strip-tags     No comments   

Issue

How can I replace contents of a tag with it's link

$str = 'This <strong>string</strong> contains a <a href="/local/link.html">local link</a>
        and a <a href="http://remo.te/link.com">remote link</a>';
$str = strip_tags($str,'<a>'); // strip out the <strong> tag
$str = ?????? // how can I strip out the local link anchor tag, but leave the remote link?
echo $str;

Desired output:

This string contains a local link and a <a href="http://remo.te/link.com">remote link</a>

Or, better yet, replace contents of remote link with its url:

This string contains a local link and a http://remo.te/link.com

How can I achieve the final output?


Solution

To replace your remotely linked anchor with the URL:

<a href="(https?://[^"]+)">.*?</a>
$1

To remove the anchor around a local URL:

<a href="(?!https?://)[^"]+">(.*?)</a>
$1

Explanation:

Both expressions match <a href=", ">, and </a> literally. The first one will then match a remote URL (http, optional s, :// and everything up to the closing ") in a capture group that we can reference with $1. The second expression will match anything that does not start with the protocol used previously, and then capture the actual text of the link into $1.

Please note that regular expressions aren't the best solution to parsing HTML, since HTML is not a regular language. However, it seems like your use case is "simple" enough that we can make a regular expression. This will not work with links like <a href=''></a> or <a href="" title=""></a>, but it can be expanded on to allow for these use cases (hence my previous note of HTML not being regular).


PHP

$str = 'This <strong>string</strong> contains a <a href="/local/link.html">local link</a> and a <a href="http://remo.te/link.com">remote link</a>';
$str = strip_tags($str,'<a>');

$str = preg_replace('~<a href="(https?://[^"]+)".*?>.*?</a>~', '$1', $str);
$str = preg_replace('~<a href="(?!https?://)[^"]+">(.*?)</a>~', '$1', $str);

echo $str;
// This string contains a local link and a http://remo.te/link.com


Answered By - Sam
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing