我有一个像这样的字符串
"hello my #old good #friends"
我希望将某些单词(如#example)转换为HTML链接
"hello my <a href='example.com/tags/old'>old</a> good <a href='example.com/tags/friends'>friends</a>"
由于
抱歉我的英文
答案 0 :(得分:1)
使用没有正则表达式的PHP
class Talker {
private name: string = 'Some talker';
constructor(private $q) {}
public deferredHello() {
this.$q.when('Hi, I\'m ' + this.name);
}
}
angular.module('test').service('Talker', function($q) {
return new Talker($q);
});
html输出
class Talker {
private name: string = 'Some talker';
constructor(private $q, money: number) {}
public deferredHello() {
this.$q.when('Hi, I\'m ' + this.name);
}
}
angular.module('test').service('Talker', function($q) {
// we can ask for more parameters if needed
return function talkerFactory(money) { // return a factory instead of a new talker
return new Talker($q, money);
};
});
angular.module('yatest').service('Organiser', function(Talker) {
let talker = Talker(42); // will call talkerFactory and return a new instance of talker
})
答案 1 :(得分:1)
使用php你可以在下面做(最简单快速): -
使用create
: -
keyboardCharMap and keyboardNameMap are the key to making this work
输出: - https://eval.in/595251
使用preg_replace()
你的工作实例给出了很多答案。检查一下。
答案 2 :(得分:0)
在PHP中使用preg_match和str_replace可以执行此操作:
$someText = "hello #old things";
//Returns an array of hashtags
preg_match("/#(\w+)/", $someText, $matches);
//Generate an array of links from the hashtags
$links = [];
foreach ($matches as $hashtag)
{
$link = "<a href=\"site.com/tag/{$hashtag}\">{$hashtag}</a>";
array_push($links, $link);
}
//replace the arrary of hashtags in our string with the link
$string = str_replace($matches, $links, $someText);
现在可能有一种方法可以在里面进行字符串替换,但这就是我们立刻想到的。我会查看字符串替换,然后根据我的发现进行编辑。
编辑:你想用php或js吗?
答案 3 :(得分:0)
拆分字符串,改变它并再次添加。
<table border="1">
<thead>
<tr>
<th>Name</th>
<th>URL</th>
</tr>
</thead>
<tbody>
<tr>
<td>Test</td>
<td>http://www.exampledomain.org/How-We-Work/General-Information/General-Opportunities/Open-Concept-Memo-Global-Test-Cases</td>
</tr>
</tbody>
</table>
<table border="1">
<thead>
<tr>
<th>Name</th>
<th>URL</th>
</tr>
</thead>
<tbody>
<tr>
<td>Another Test</td>
<td>http://www.exampledomain.org/search?q=flagship+collaborative+research+program&btnG=Google%2BSearch&client=csiro_frontend&output=xml_no_dtd&proxystylesheet=csiro_frontend&proxyreload=0&sort=date%253AD%253AL%253Ad1&wc=200&wc_mc=1&oe=UTF-8&ie=UTF-8&ud=1&exclude_apps=1&site=Main&filter=0&getfields=*&sourcepage={CB41B120-BEE8-4511-9BED-A5E43D32381D}</td>
</tr>
</tbody>
</table>
var converted = str.split(' ').map(function(st){
if(st.indexOf('#') !== -1){
st = '<a href="example.com/tags/' + st.replace('#', '') + '">' + st.replace('#', '') + '</a>';
}
return st;
});
converted.join(' ');
var str = "hello my #old good #friends";
$('#change').on('click', function() {
var converted = str.split(' ').map(function(st) {
if (st.indexOf('#') !== -1) {
st = '<a href="example.com/tags/' + st.replace('#', '') + '">' + st.replace('#', '') + '</a>';
}
return st;
});
$('#test').html(converted.join(' '));
})
//"hello my <a href='example.com/tags/old'>old</a> good <a href='example.com/tags/friends'>friends</a>"
答案 4 :(得分:0)
总是匆匆忙忙地回答一下,这可能有点过头或者不是最聪明的方式......但是这里有:
<?php
//
$string1="hey #bro this is #such a cool #answer";
$string2="hey bro this is such a cool answer";
//
print "\n String1 == ".LinkifyHashtags($string1);
print "\n String2 == ".LinkifyHashtags($string2);
//
function LinkifyHashtags($string)
{
//
$matches=true;
//
$string_linkified=$string;
// Grab all hashtags
if(Preg_Match_All('@\#(?P<hash_word>\w+)@',$string,$matches)){
// Iterate over all matches
foreach($matches['hash_word'] as $word){
$word_ent=HtmlEntities($word,ENT_COMPAT,'utf-8');
$word_enc=UrlEncode($word);
$string_linkified=Str_Replace("#{$word}","<a href=\"/tags/{$word_enc}\">{$word_ent}</a>",$string_linkified);
}
}
return $string_linkified;
}
?>
答案 5 :(得分:0)
Jquery版本:
$(function(){
var test = 'hello my #old good #friends';
test = test.split(' ');
test = test.map(function(item){
return item.indexOf('#') > -1?"<a href='example.com/tags/"+item.slice(1)+"'>"+item.slice(1)+"</a>":item;
});
$('#divResult').html(test.join(' '));
});
答案 6 :(得分:0)
$(document).ready(function(){
var words = 'hello my #old good #friends';
var wordarr = words.split(' ');
var str = '';
$.each(wordarr,function(i,val){
if(wordarr[i].indexOf('#') == 0){
wordarr[i] = '<a href="example.com/tags/'+wordarr[i].replace("#", "")+'">'+wordarr[i].replace("#", "")+'</a>';
str += wordarr[i]+' ';
}else{
str += wordarr[i]+' ';
}
});
$('#result').html(str);
});