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='http://www.google.com' 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='http://www.google.com' onclick='return check()'>check</a>
<script type='text/javascript'>
function check()
{
return false;
}
</script>
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>
</div>
</a>
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
a.imgLink{
color: white; text-decoration: none;
}
div.imgLink{
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
</div>
</a>
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
Oauth Implementation in Netsuite Using PHP
How to Pass Value from One PHP Page to Another Using Session
Phpmailer - the Following Smtp Error: Data Not Accepted
How to Include a PHP.Ini File in Another PHP.Ini File
Laravel Model Callbacks After Save, Before Save, etc
PHP Include Causes White Space at the Top of the Page
PHP String Interpolation Syntax
Pdo SQLite Could Not Find Driver... PHP File Not Processing
Array to Object and Object to Array in PHP - Interesting Behaviour
Adding a Namespace When Using Simplexmlelement
Decrypting Strings in Python That Were Encrypted with Mcrypt_Rijndael_256 in PHP
How to Perform an Action Every 5 Results
How to Execute PHP with Extension File.Php.Jpg
PHP Get Dropdown Value and Text
Jquery Mobile: Sending Data from One Page to the Another