Convert Clickable Anchor Tags to Plain Text in HTML Document

Convert clickable anchor tags to plain text in html document

You can make the match ungreedy using ?.
You should also take into account there may be attributes before the href attribute.

$result = preg_replace('/<a [^>]*?href="(http:\/\/[A-Za-z0-9\\.:\/]+?)">([\\s\\S]*?)<\/a>/',
'<strong>\\2</strong> [\\1]', $content);

Replace anchor tag with href in parenthesis

You can use Regex in PHP:

$input_string = "Please go to <a href='' style='color:red'>Google</a> and search.";
$pattern = "/<a[\w\s\.]*href='([\w:\-\/\.]*)'[\w\s\.\=':>]+<\/a>/";
$replacement = '(\1)';
$output_string = preg_replace($pattern, $replacement, $input_string);

This regular expression matches the entire a tag with it's contents and standalone href value. Then simple preg_replace function replaces matched a tags with matched href values.

HTML anchor tag with Javascript onclick event

If your onclick function returns false the default browser behaviour is cancelled. As such:

<a href='' onclick='return check()'>check</a>

<script type='text/javascript'>

function check()
return false;


Either way, whether google does it or not isn't of much importance. It's cleaner to bind your onclick functions within javascript - this way you separate your HTML from other code.

html, displaying a link as normal text

If you have a look at Cascading Style Sheets (CSS) you can change the colour and the text style of the link.

In your example, you could use

<a id="" href="" target="_parent" style="color: white; text-decoration: none;"><img src="" width="121" height="20" alt="Sample Image">
<div style="position:absolute; sleft:163px;top:1px;font-size: 12px; display: block">
<font color="white">Log in</font>

However I would learn how to use external stylesheets and link them to your HTML through the <link> tag in the <head> of your html. You can then style up individual tags through the tag name, an id or a css class. So an updated example would be:

<link rel="stylesheet" href="link-to-your-css-file" />

in your css file have

color: white; text-decoration: none;
position: absolute; left: 163px; top: 1px; font-size: 12px; display: block;

Then your html would be

<a class="imgLink" id="" href="" target="_parent">
<img src="" width="121" height="20" alt="Sample Image">
<div class="imgLink">
Log In

Not only does it make your HTML "dry" but it gives you greater control over the styles of your html by only changing the css file.

Convert a line of text generated in a javascript file into a clickable link in HTML

If you want to navigate to that URL, you don't need to "click the link", you can just change the location of the window:

window.location.href = links[randomLink];

If you want to make a link to that location, so people can click it, make that <div> an <a>

<a id="linkDisplay" href='#' />

(the # means it will just go to the top of the page) and then update the href as needed:

document.getElementById('linkDisplay').href = links[randomLink];

Related Topics

Leave a reply