PHP正则匹配字符串中的标签

如果标签里面有中文、英文、数字等混排,则需要对汉字进行特殊处理,由于PCRE不支持\U \P \L之类的perl字符串处理转义,使用16进制或者Unicode进行处理,范例如下:

[coolcode lang=”php”]

$str = “之二宽阔的甘家口东#标签1#标签2 #标签3。#标签4,都发$1234 ¥xc,cvm , ¥12,dflksjf如何#标签5.x #tag6.cvxcv“”;
preg_match_all(‘/#([a-zA-Z0-9\x7f-\xff]+)/’, $str, $mat);
print_r($mat);

preg_match(“/[\x{00a5}\x{ffe5}](\d+)/u”, $str, $mat);
print_r($mat);

[/coolcode]

得到的结果如下:


Array
(
[0] => Array
(
[0] => #标签1
[1] => #标签2
[2] => #标签3。
[3] => #标签4
[4] => #标签5
[5] => #tag6
)

[1] => Array
(
[0] => 标签1
[1] => 标签2
[2] => 标签3。
[3] => 标签4
[4] => 标签5
[5] => tag6
)

)
Array
(
[0] => ¥12
[1] => 12
)

补充说明:

双字节字符编码范围

1. GBK (GB2312/GB18030)
x00-xff GBK双字节编码范围
x20-x7f ASCII
xa1-xff 中文 gb2312
x80-xff 中文 gbk

2. UTF-8 (Unicode)

u4e00-u9fa5 (中文)
x3130-x318F (韩文
xAC00-xD7A3 (韩文)
u0800-u4e00 (日文)

2 thoughts on “PHP正则匹配字符串中的标签”

  1. A formidable share, I just given this onto a colleague who was doing somewhat evaluation on this. And he the truth is purchased me breakfast as a result of I found it for him.. smile. So let me reword that: Thnx for the treat! But yeah Thnkx for spending the time to discuss this, I feel strongly about it and love reading more on this topic. If possible, as you turn out to be expertise, would you thoughts updating your weblog with more particulars? It’s extremely useful for me. Large thumb up for this weblog put up!

  2. We are a group of volunteers and starting a new scheme in our
    community. Your web site provided us with valuable info to work on. You have done a formidable job and our entire community will be thankful to you.

  3. I’ve been browsing online more than 3 hours today, yet I never found any interesting
    article like yours. It is pretty worth enough for me.
    In my view, if all web owners and bloggers made good content as you did, the net will be a
    lot more useful than ever before.

  4. hello there and thank you for your info – I have
    certainly picked up something new from right here. I did however expertise a few technical points using this site, as I experienced to reload the site a lot of times previous to I could get it to load properly.
    I had been wondering if your web host is OK? Not that I am complaining, but sluggish loading instances times will often affect your placement in google and could damage your high-quality score
    if advertising and marketing with Adwords.
    Anyway I am adding this RSS to my e-mail and could look out for much more
    of your respective interesting content. Make sure you update this again soon.

  5. Have you ever thought about writing an ebook or guest authoring on other
    sites? I have a blog based upon on the same topics you discuss and would love to
    have you share some stories/information. I know my subscribers would appreciate your work.
    If you are even remotely interested, feel free to shoot
    me an email.

  6. Thanks for your marvelous posting! I actually enjoyed reading it,
    you are a great author.I will remember to bookmark your blog and will often come back in the foreseeable future.
    I want to encourage you to continue your great posts, have a nice holiday weekend!

  7. Hi there, I discovered your website by way of
    Google even as looking for a related matter, your web site came up, it appears to be like good.
    I have bookmarked it in my google bookmarks.
    Hello there, simply become aware of your weblog thru
    Google, and found that it’s truly informative. I am gonna watch out for brussels.
    I will be grateful when you proceed this in future.
    A lot of people can be benefited from your writing. Cheers!

  8. Unquestionably imagine that that you said. Your favourite justification seemed to be at the internet the
    simplest thing to remember of. I say to you, I definitely get irked even as other folks think about
    worries that they just do not recognise about. You controlled to hit the nail upon the top as neatly
    as defined out the entire thing with no need side effect , people can take a signal.
    Will likely be again to get more. Thank you

  9. My developer is trying to persuade me to move to .net from PHP.
    I have always disliked the idea because of the expenses.
    But he’s tryiong none the less. I’ve been using WordPress on a variety of
    websites for about a year and am concerned about
    switching to another platform. I have heard good things about blogengine.net.
    Is there a way I can import all my wordpress posts into it?

    Any help would be really appreciated!

  10. Amazing blog! Do you have any helpful hints for aspiring writers?

    I’m hoping to start my own website soon but I’m a little lost on everything.

    Would you suggest starting with a free platform like WordPress or go for a paid option? There are so many options out there
    that I’m totally confused .. Any tips? Many thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image