How to create a link like Instagram username or hashtag in php or javascript ?
I used to use these codes before`
str_ireplace('#', '#', 'I #aaaa in');
`
But all the words are converted to a link.
And can i add a python library to javascript or php?
So assuming you inserted your usernames with PHP, the html structure could look similar to this.
HTML:
<p class="username">#Username</p>
<p class="username">#otherUser</p>
<p class="username">#meAndYou</p>
JavaScript + JQuery:
$(document).ready(() => {
changeUserNames();
}
function changeUserNames(){
$('p').each(function (index) {
var text = $(this).text();
var username = text.split('#')[1];
$(this).html(`#${username}`);
});
}
JS Fiddle:
https://jsfiddle.net/zy46npb2/4/
Related
I have a textarea in which I am getting user's input data. But I need to know if there is any URL in textarea and convert it to anchor tag. For example:
Textarea Data:
Hi I'm Abdul. My Website is https://website.com
After Anchor Tag:
Hi I'm Abdul. My Website is https://website.com
Currently my code is:
var status = $('#status').val();
var urlCheck = new RegExp("([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+#)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?(/.*)?");
if(urlCheck.test(status)) {
alert("url inside");
console.log(urlCheck.exec(status)[0]);
}
This is my current code but I don't know how to replace url with anchor tag in that string.
I am not sure if i understand you correctly, but do you want to have it changed live or after the form was sent? If the latter, i would try something like this:
var urlCheck = new RegExp("([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+#)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?(/.*)?");
if(urlCheck.test(status)) {
alert("url inside");
console.log(urlCheck.exec(status)[0]);
// Here my possible solution (not tried out)
$('#status').val('<a href="http://'+urlCheck.exec(status)[0]+"' target='_blank'>the link</a>");
}
But this would also mean that you could/must check with a RegEX if the user entered http or not.
var status = $('#status').text();
var urlCheck = new RegExp("([a-zA-Z0-9]+://)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+#)?([a-zA-Z0-9.-]+\\.[A-Za-z]{2,4})(:[0-9]+)?(/.*)?");
if(urlCheck.test(status)) {
alert("It has an URL!");
console.log(urlCheck.exec(status)[0]);
}
document.getElementById("status").innerHTML = status.replace(urlCheck.exec(status)[0],"<a href='"+urlCheck.exec(status)[0]+"'>"+urlCheck.exec(status)[0]+"</a>");
<div id="status">Hi I'm Abdul. My Website is https://website.com</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
We can use the html.replace() to replace the url inside the tags we wanted.
You have to use the JS replace() function.
I set the following example with an input textarea and an output textarea for let you see the difference.
function addUrl() {
var status = $('#status').val();
var urlCheck = /(([a-zA-Z0-9]+:\/\/)?([a-zA-Z0-9_]+:[a-zA-Z0-9_]+#)?([a-zA-Z0-9.-]+\.[A-Za-z]{2,4})(:[0-9]+)?(\/.*)?)/;
$('#output').val(status.replace(urlCheck, '$1'));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<label for="status">Input</label>
<textarea id="status" onChange="addUrl()"></textarea>
<br/>
<label for="output">Output</label>
<textarea id="output"></textarea>
use linkifyHtml or linkifyString : Linkify String Interface. Use linkify-string to replace links in plain-text strings with anchor tags.
I have the following code below. I want to change the H2 message No results were found for this query: <em class="querytext">to something like "No results were found by hello world! without hard coding as I have no control of the piece of text in an HTML file, is their any way I can do this via an if condition CSS or JS to read the string then change the message on load of the page? something like if text == No results were found for this query: display "No results were found by hello world!.
<div class="search-results">
<h2>No results were found for this query: <em class="querytext"></em></h2>
</div>
You could try DOM manipulation with JavaScript:
<html>
<head>
<title>Title of the document</title>
</head>
<body>
<script type="text/javascript">
function updateResultText(text) {
var resultTextEl = document.querySelector('.search-results > h2');
if(resultTextEl) {
resultTextEl.innerText = "No results were found by " + text;
}
}
</script>
<button onclick="updateResultText('hello world!')">Update Text</button>
<div class="search-results">
<h2>No results were found for this query: <em class="querytext"></em>
</h2>
</div>
</body>
</html>
The JS you need is below:
function updateResultText(text) {
var resultTextEl = document.querySelector('.search-results > h2');
if(resultTextEl) {
resultTextEl.innerText = "No results were found by " + text;
}
}
You would then make the call to updateResultText with whatever you want the text following 'by ' to consist of.
Here's a solution that uses vanilla JS to change all h2 that are child of .search-results class:
for (let h2 of document.querySelectorAll('.search-results > h2')) {
if (h2.textContent.search('No results were found for this query') >= 0)
h2.textContent = 'No results were found by hello world!';
}
<div class="search-results">
<h2>No results were found for this query: <em class="querytext"></em>
</h2>
</div>
old JavaScript
var query = "" // Get the username from either the backend, URL, or any other way like simple login form to the local storage without backend
var newText = 'No results were found for this' + query ': <em class="querytext"></em></h2>'
var el = document.querySelector('.search-results h2').innerHTML = newText
ES5
const query = "" // Get the username from either the backend, URL, or any other way like simple login form to the local storage without backend
const newText = `No results were found for this ${query}: <em class="querytext"></em></h2>`
const el = document.querySelector('.search-results h2').innerHTML = newText
At end of your page put the following script tag :
<script type="text/javascript">
var elem= document.querySelector('.search-results > h2');
if(elem) {
if(elem.innerText.indexOf("No results were found for this query")>=0)
elem.innerHtml="anything You want !!";
}
}
</script>
then after that there should be </body></html> tags.
I'm using uncode theme and I have a page heading that is showing 'Archive: Portfolio'
I want to remove the 'Archive:' bit from that heading.
In the source it looks like this:
<h1 class="header-title h1"><span>Archives: Projects</span></h1>
I have tried removing Archive from all the page titles with Yoast SEO plugin but it is still showing.
Is there a way to remove that word with javascript maybe does anyone know?
Thanks!
I'd be wary in removing it via javascript. It seems to me that adding a piece of text somewhere in the code's execution, and then removing it on the client-side smells like "contrived complexity".
Take a look at the wordpress template hierarchy, and manually search for the template file that's rendering the Archives: string of text.
I'd start with archive.php, and then fall my way up through other archive-*.php pages, then to taxonomy.php category.php, and so on.
If you're comfy in the command line, you might also consider grepping for the string: grep -r /path/to/wp/theme "Archive:" and sifting through the results to find the template file(s) with that on one of their lines.
But if you insist on removing the string via javascript, you might try dropping something like this at the bottom of the <body>, via a function in functions.php:
function remove_archive_text_via_js() {
if (is_archive()) { ?>
<script type="text/javascript">
var archiveHeaders = document.getElementsByClassName('header-title');
for (i = 0, headerCount = archiveHeaders.length; i < headerCount; i++) {
var replacedText = archiveHeaders[i].textContent.replace('Archives: ', '');
archiveHeaders[i].textContent = replacedText;
}
</script>
<?php }
}
add_action('wp_footer', 'remove_archive_text_via_js');
var elem = document.getElementsByClassName('header-title h1');
var innerSpan = elem[0].getElementsByTagName('span');
innerSpan[0].innerHTML = innerSpan[0].innerHTML.replace('Archives: ', 'jsfiddle');
jsfiddle: https://jsfiddle.net/orcadj3u/
$(function() {
$( "h1 span" ).each(function( index ) {
var newtext = $(this).text().replace("Archives: ", " ");
$(this).html(newtext);
});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<h1 class="header-title h1"><span>Archives: Projects</span></h1><br>
<h1 class="header-title h1"><span>Archives: Solutions</span></h1><br>
<h1 class="header-title h1"><span>Archives: Yozgat</span></h1><br>
<h1 class="header-title h1"><span>Archives: Turkey</span></h1><br>
I have more than 2000 email addresses. which i have exported from feedburner.
And the email address look like below;
adminvicky#gmail.com Active 12/05/2015 03:07
adminvishal250#gmail.com Pending Verification 8/05/2015 01:07
I want to extract email address from the text file by removing Active, Pending Verification, Date [i.e. 8/05/2015] and time [i.e 03:07] using JavaScript.
I have created a JavaScript Program which something like below which working properly for removing Active, Pending verification text,
<script>
function extracter() {
var a = document.getElementById('input').value;
document.getElementById('output').innerHTML =
a.replace(/Active|Pending|Verification| /g, '');
}
</script>
<textarea id="input"></textarea><br/>
<br/>
<input type="button" value="click" onclick="extracter()"/>
<br/>
<br/>
<textarea id="output"></textarea>
And the output is,
adminvicky#gmail.com 12/05/2015 03:07
adminvishal250#gmail.com 8/05/2015 01:07
And I want the below output. Just help me to remove "Date" and "Time",
adminvicky#gmail.com
adminvishal250#gmail.com
Try this one, i think it will do the job
var a = document.getElementById('input').value;
document.getElementById('output').innerHTML = extractEmails(a).join('\n');
And the function:
function extractEmails (text)
{
return text.match(/([a-zA-Z0-9._-]+#[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
}
Here is a fiddle
Here is also an example using jQuery also Extract all email addresses from bulk text using jquery
Try to use this regex:
([a-zA-Z0-9._-]+#[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)
REGEX DEMO
In your Javascript you can implement it like this:
function getMail ( text ){
return text.match(/([a-zA-Z0-9._-]+#[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
}
JSFIDDLE DEMO
you can easily write a regex and iterate over the results like:
var reg = new RegExp(/^[\w-\.]+#([\w-]+\.)+[\w-]{2,4}$/g);
var email;
while((email = reg.exec(targetText)) !== null) {
// do something with the email
}
Let's try with this simple regular expression:
var record = ' adminvicky#gmail.com Active 12/05/2015 03:07';
var regExp = /^\s*(.*?)\s+/;
console.log(record.match(regExp)[1]);
You can try this regex instead:
a.replace(/\s+.+$/g, '')
This should work for your case.
I would use string.split(" ") and split the textfile at its spaces.
Example:
var string = " adminvicky#gmail.com Active 12/05/2015 03:07 adminvishal250#gmail.com Pending Verification 8/05/2015 01:07"
var array = string.split(" ");
var emails = [];
for(var i = 0; i < array.length; i++){
if(array[i].indexOf("#") != -1){
emails.push(array[i]);
}
};
Then you have an array emails which contains your email adresses.
Using JQuery load function to read content from .txt file and display email as hyperlink:
$(document).ready(function(){
//Get the text content from txt file using load function
$( "#divid" ).load( "/xyz.txt",function(response, status, xhr){
if(status=='success') {
/* After loading the static text, modifying the email address to hyper link */
var corrected = response;
var emailRegex =/[a-zA-Z0-9._-]+#[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}/g;
corrected.match(emailRegex).forEach(function(email) {
console.log(email);
corrected = corrected.replace ( email, '' + email + '' );
});
$('#divid').html(corrected);
}
});
});
How to find and replace begin tag and end tags in an html string using javascript/jquery
for example
var myString = "<span> Hello john</span><div> John likes to play guitar </div> <div style="color:Blue;font-weight:bold" class='quotes'>Anna likes to arrange flowers
</div>";
I need to find the "div" tag and replace with other html tag like "p" tag/ "span" tag
Resulting html string after replace "div" tag to "p" tag
var replacestring="<span> Hello john</span><p> John likes to play guitar </p> <p style="color:Blue;font-weight:bold" class='quotes'>Anna likes to arrange flowers
</p>";
Please suggest any solution.
Javascript with regular expression:
myString = myString.replace(/<(\/)?div[^>]*>/g, '<$1p>');
Also see my jsfiddle.
=== UPDATE ===
myString = myString.replace(/<(\/)?div([^>]*)>/g, '<$1p$2>');
jsfiddle 2.
myString = $('<div />').html(myString).find('div').replaceWith(function() {
var p = $('<p />');
p.html($(this).html());
$.each(this.attributes, function(index, attr) {
p.attr(attr.name, attr.value);
});
return p;
}).end().html();
jsFiddle.
Trying something with jQuery, however, I am not sure if it is any better than what alex suggested.
var $replaceString=$(replaceString);
$("div",$replaceString).each(
function()
{
var $p=$(document.createElement('p')).html($(this).html());
//add code to add any attribute that you want to be copied over.
$(this).after($p);
$(this).remove();
}
);