简体   繁体   English

PHP正则表达式检测@twitter然后创建url

[英]PHP regular expression to detect @twitter then create url

I would like to detect a string with @account/status/postid 我想用@ account / status / postid检测一个字符串

I have found a solution to detect the @account and create the link as such: 我找到了一种检测@account并按如下方式创建链接的解决方案:

$str = preg_replace("/@(\w+)/", "<a href=\"http://www.twitter.com/\\1\" target=\"_blank\">@\\1</a>", $str);

However, I need the regular expression to include /status/postid and include this in the link, the final output should be: 但是,我需要正则表达式包含/status/postid并将其包含在链接中,最终输出应为:

<a href="http://www.twitter.com/account/status/postid" target="_blank">@account</a>

What I have tried (as I said I am completely lost and cannot find a solution online): 我尝试过的方法(就像我说的那样,我完全迷路了,无法在线找到解决方案):

preg_replace("/@(\w+)/|$", "<a href=\"http://www.twitter.com/\\1\2\" target=\"_blank\">@\\1</a>", $str);

preg_replace("/@(\w+)/.\2", "<a href=\"http://www.twitter.com/\\1\2\" target=\"_blank\">@\\1</a>", $str);

preg_replace("/@(\w+)/./(\w+)", "<a href=\"http://www.twitter.com/\\1\2\" target=\"_blank\">@\\1</a>", $str);

preg_replace("/@(\w+)./(\w+)", "<a href=\"http://www.twitter.com/\\1\2\" target=\"_blank\">@\\1</a>", $str);

edit: 编辑:

The structure of $str will be @TwitterAccountName/status/PostID $ str的结构将为@ TwitterAccountName / status / PostID

status will always be /status/, TwitterAccountName varies, as does PostID status始终为/ status /,TwitterAccountName和PostID相同

The hypertext reference needs to be twitter.com/TwitterAccountName/status/PostID 超文本引用必须是twitter.com/TwitterAccountName/status/PostID

The text within the anchor tags should just be @TwitterAccountName 定位标记中的文本应该只是@TwitterAccountName

What identifies the string as a twitter page that needs to be linked to, is the @ 将字符串标识为需要链接的Twitter页面的是@

In case you are still interested in an answer, this should do the trick: 如果您仍然对答案感兴趣,请按以下步骤操作:

echo preg_replace(
     '|((@\w+)/\w+/\w+)|',
     '<a href="http://www.twitter.com/\\1" target="_blank">\\2</a>',
     $str);

Similar to chris85 I don't use twitter so I am not totally sure of the type of strings to expect for status and postid. 与chris85类似,我不使用Twitter,所以我不确定要确定状态和postid的字符串类型。 I assume that they are made up of "word"-characters ( \\w ). 我假定它们由“单词”字符( \\w )组成。 You can of course change that if necessary. 您当然可以根据需要更改它。

One important difference from my regexp-string to yours is, that I delimited the actual regexp with a different character from / to | 我的正则表达式字符串与您的正则表达式字符串的一个重要区别是,我用/|的不同字符来分隔实际的正则表达式| since the slash ( / ) would have to be masked all the time (like \\/ ) and that is very tedious ( preg_replace accepts any character as the regexp delimiter). 因为斜杠( / )必须一直被屏蔽(例如\\/ ),而且这非常繁琐( preg_replace接受任何字符作为正则表达式定界符)。

My test string 我的测试字符串

$str='this is a test sentence with a @twaccount/twstatus/twpostid and some more words afterwards.'

gets changed into: 变成:

'this is a test sentence with a <a target="_blank"
href="http://www.twitter.com/@twaccount/twstatus/twpostid"></a> 
and some more words afterwards.' // (no line breaks)

With preg_replace() the default behaviour is to replace all occurences of the search pattern. 使用preg_replace()的默认行为是替换所有出现的搜索模式。 A global-flag like g in other regexp versions is not necessary (and will cause an error). 在其他正则表达式版本中,不需要像g这样的全局标志(这将导致错误)。 A limit to the count of replacements can be set with an optional fourth parameter of the function. 可以使用该功能的第四个可选参数设置替换次数的限制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM