How to get a matching word surrounded by hash tags with PHP

如何获得一个匹配的词被散列和PHP标签吗

问题 (Question)

I have something similar to this:

<a href="#action_link#" target="_blank">Token1</a><br>
<a href="##action_link##" target="_blank">Token1</a><br>

And I need to get only the one(s) that is/are surrounded by only one hashtag (#), for this sample I need only the first anchor tag.

I have tried also with grabbing the href attr to filter and make the regexp more easier with something like this:

preg_match_all('/<a\s[^>]*href=(\"??|\'??)(#action_link#?)[^>]*>/siU', $someHTML, $match);

But with no success :(

Could you please help me?

我有一些类似于:

<a href="#action_link#" target="_blank">Token1</a><br>
<a href="##action_link##" target="_blank">Token1</a><br>

我只需要一个(s)/四周都是只有一个标签(#),对于此示例我只需要第一个锚标记。

我也试图抓住href attr过滤,使正则表达式更容易与是这样的:

preg_match_all('/<a\s[^>]*href=(\"??|\'??)(#action_link#?)[^>]*>/siU', $someHTML, $match);

但是没有成功:

你能帮我吗?

最佳答案 (Best Answer)

You can use:

preg_match('/\"\#(.*)\#\"/im', $string, $matches);
print_r($matches);

Where $string is your string to search

您可以使用:

preg_match('/\"\#(.*)\#\"/im', $string, $matches);

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/16925952